WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This sql query

$wpdb->query("UPDATE log SET total=total+1 WHERE id='1')");

How do I turn it into $wpdb->update() statement?


$wpdb->update('log', array('total'=>'total+1'), array('id'=>'1'));
share|improve this question
up vote 4 down vote accepted

You don't. Not with the WPDB update() function. The update function assumes that the values of the columns are strings. You can override that with the format parameter, but that only allows %d, %f, and %s (integer, float, string). It doesn't allow operations on columns like total+1.

You'd have to use the query() function directly to do your update.

share|improve this answer
I do try many method, still not work. $wpdb->query should be the only way, Thanks – supersuphot Sep 22 '12 at 10:59

By using the query method, you may risk a sanitization problem. By using the update method, you risk a race condition. To overcome both, just before update issue a select on the row with a final clause "FOR UPDATE". For fetching the results you can use the same condition which you are going to use for update and get_row method.

The only caveat being if your condition hits multiple rows, there is no other way than to use query method of wpdb.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.