Incrementing database field – value jumps by 2 x value
-
I’ve run into a strange problem. I have a database field that contains an integer, and I’d like to increment it by one. However, every method I’ve tried increments it by 2. For example, this SQL works in phpMyAdmin:
UPDATE my_test SET counter = (counter + 1) WHERE id = 7
However, if placed in a call to $wpdb->query, it increments the value by 2. So then I tried this:
$mycount = $wpdb->get_var( 'SELECT counter FROM my_test WHERE id = 7); $mycount = $mycount+1; $wpdb->update(...) OR $wpdb->query( 'UPDATE...'); $newcount = $wpdb->get_var( 'SELECT counter FROM my_test WHERE id = 7);
In both cases (update or query) $newcount is equal to $mycount+1, as you would expect. However, the database value is actually $mycount+2.
Calling query or update with a “new” value works fine; it’s just the increment operations that are misbehaving.Could this be related to datatype? String vs. integer? Do I need to typecast the values? I’m sure I’ve missed something obvious and I’m hoping a fresh set of eyes will see it! Thanks in advance for any suggestions/help.
- The topic ‘Incrementing database field – value jumps by 2 x value’ is closed to new replies.