Page 1 of 1

Updating a row... +=1

Posted: Fri Oct 05, 2007 5:23 pm
by figaro11
How can I update a tables rows field to equal to the current value plus 1? In other words Something like this:

Code: Select all

UPDATE registry SET value += 1 WHERE class = 'Hit Counter' LIMIT 1;
It's hard for me to explain, and I'm not very SQL savvy. So hopefully you get the idea.

Posted: Fri Oct 05, 2007 5:24 pm
by John Cartwright
Close ;)

Code: Select all

UPDATE `registry` SET `value` = `value` + 1 WHERE `class` = 'Hit Counter' LIMIT 1; 

Posted: Fri Oct 05, 2007 6:39 pm
by figaro11
Jcart wrote:Close ;)

Code: Select all

UPDATE `registry` SET `value` = `value` + 1 WHERE `class` = 'Hit Counter' LIMIT 1; 
Sweet! But why quote the table/field names?

Posted: Fri Oct 05, 2007 7:24 pm
by feyd
To protect against the use of keywords as database, table and field names. Although one should simply not use keywords or potential future keywords.

Posted: Fri Oct 05, 2007 7:51 pm
by figaro11
feyd wrote:To protect against the use of keywords as database, table and field names. Although one should simply not use keywords or potential future keywords.
Oooooooh. I see.

But I get this error when I do use qoutes:
Warning: Wrong parameter count for mysql_query() in /home/content/html/lib/php/hit-counter.php on line 7

Posted: Fri Oct 05, 2007 7:56 pm
by feyd
Adding backticks wouldn't create such an error.

Posted: Fri Oct 05, 2007 8:08 pm
by figaro11
feyd wrote:Adding backticks wouldn't create such an error.
What are backticks?

Posted: Fri Oct 05, 2007 8:15 pm
by feyd
The type of quotes Jcart used above.

Note the difference: ` versus '

Posted: Sat Oct 06, 2007 4:31 am
by onion2k
These days I go further than just using backticks, I use the fully qualified table column pair. Eg

Code: Select all

UPDATE `registry` SET `registry`.`value` = `registry`.`value` + 1 WHERE `registry`.`class` = 'Hit Counter' LIMIT 1;
It makes no difference in a simple statement like that one, but in a select with 10 tables joined together I find it really improves readability.