LemonInflux Posted March 24, 2008 Share Posted March 24, 2008 <?php $query = mysql_query("SELECT * FROM `table` WHERE `id` = '1'") or die(mysql_error()); while($row = mysql_fetch_assoc) { $sql = mysql_query("UPDATE `table` SET `field` = '". $row['field'] + 1 ."' WHERE `id` = '1'"); } ?> There are probably a hundred mistakes, but this is an example. I'm trying to add 1 to a field's value, but the only way I can think how to do it is to start a loop and then use $row['field'] + 1. Surely there's a quicker way? (For example, $v + 1 is the same as $v++ in PHP) Quote Link to comment Share on other sites More sharing options...
fotobleu Posted March 24, 2008 Share Posted March 24, 2008 hi this might help http://www.tech-recipes.com/rx/2139/mysql_increment_an_exisitng_value Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted March 24, 2008 Author Share Posted March 24, 2008 Will the same kind of thing work in MSSQL? I'm building software with support for multiple types of database, so if it doesn't work for like, MySQL, MSSQL and PostgreSQL, I can't use it :{ Quote Link to comment Share on other sites More sharing options...
fotobleu Posted March 24, 2008 Share Posted March 24, 2008 i dont know about that, but you can test it , hope it works Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted March 24, 2008 Share Posted March 24, 2008 UPDATE table SET field = table.field+1 WHERE id=table.id someone will need to check this because its just a guess, however it should work for you. gdlk Quote Link to comment Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 UPDATE table SET field = table.field+1 WHERE id=table.id someone will need to check this because its just a guess, however it should work for you. gdlk That will update all rows (id always equals id) <?php $id = 1; mysql_query ("UPDATE table SET field = field+1 WHERE id = $id"); MySQL, MSSQL compatible. I don't use postgres but should be OK. You will probably only have difficulty across dbms when you try using SQL functions, especially datetime ones. Date formats can also be a problem with different dbms Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted March 24, 2008 Author Share Posted March 24, 2008 For the sake of tidiness, would: UPDATE `table` SET `field` = 'field+1' WHERE `id` = '$id' also work (with the ` and ' added)? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 that will set field to the string value "field+1". Use UPDATE `table` SET `field` = `field`+ 1 WHERE `id` = '$id' But now it is no longer compatible with MSSQL. The equivalent MSSQL (to allow spaces/resrved words in column names) is UPDATE [table] SET [field] = [field] + 1 WHERE [id] = $id (Note MySQL allows '$id' even if $id is numeric; other dbms do not. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted March 24, 2008 Author Share Posted March 24, 2008 Ok, thanks! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 24, 2008 Share Posted March 24, 2008 Since you asked about other database types, the back-tick ` is mysql specific and will result in a syntax error with other databases. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted March 24, 2008 Author Share Posted March 24, 2008 so if I get rid of ` and ', it'll work on MySQL/MSSQL/PostgreSQL? Edit: Post 600 =D Quote Link to comment Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 So long as table/col names do not contain spaces or reserved words, the version I gave originally should work with all Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted March 24, 2008 Author Share Posted March 24, 2008 Great! Well, now to go through all the files in my project and get rid of ` and ' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.