• Resolved rwdrummond

    (@rwdrummondcoxnet)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • webdesignerchristian

    (@webdesignerchristian)

    why is
    $mycount equaled to 2 different staments

    $mycount = $wpdb->get_var( ‘SELECT counter FROM my_test WHERE id = 7);
    $mycount = $mycount+1;

    try

    $mycount = $wpdb->get_var( ‘SELECT counter FROM my_test WHERE id = 7);
    $mynewcount = $mycount+1;

    Thread Starter rwdrummond

    (@rwdrummondcoxnet)

    I think you may have missed my point. But to answer your question, it’s an inelegant way to retrieve a value and increment it.

    With your suggestion, same results: Lets say that the field ‘counter’ conatins 3. So $mynewcount is now 4. When I update with ‘counter’ => $mynewcount the value of ‘counter’ is now 5 rather than 4, which is what I expected.

    I’ve tried this and many other permutations. I can update fields just fine, but whenever I retrieve the value, manipulate it in some way (add or subtract to it) and update it, the manipulation is processed twice. If I retrieve 5 and add 7 to it, after updating it’s 19 instead of 12. I even tried passing the value around through multiple variables, to no avail.

    webdesignerchristian

    (@webdesignerchristian)

    can u pastebin

    Thread Starter rwdrummond

    (@rwdrummondcoxnet)

    Further investigation links this problem to a plugin, so I’m closing this and will address it with the plugin developer.

    Really frustrating to find a relevant sounding thread in a search and marked as resolved no less to read through and find that there is no resolution in the thread and not even a lead from the OP as to which is the relevant plugin.

    Just sayin’

    Thread Starter rwdrummond

    (@rwdrummondcoxnet)

    The plugin is WordPress Photo Album Plus (WPPA+), I probably should have specified that.

    The issue proved to be a bug in the plugin, which the developer resolved. Since the error and fix did not involve WP itself, it didn’t occur to me to come back and update this post.

    Sorry for the frustration. I do appreciate your comment as it made me look at this from another viewpoint – that will help me make better posts in the future. Cheers.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Incrementing database field – value jumps by 2 x value’ is closed to new replies.