I am using $wpdb->update to update a custom table I have in my database. When I var_dump the result it returns:

int(0)

So I tried $wpdb->print_error() to see what's wrong. However, it shows nothing. I also tried $wpdb->show_errors(), but again it showed nothing.

The wpdb docs don't go into much detail on how to use these functions, so I am unsure if I am using them correctly. But why would the result of updating a table return 0, and not show any errors?

link|improve this question

50% accept rate
feedback

5 Answers

I would recommend running the following code right after your query to see what's happening:

exit(var_dump($wpdb->last_query));

This should print the last query that hit your database. In cases like these, I usually will manually run such a query through phpMyAdmin to see if it runs without errors and to see if it even affects the database. Additionally, by seeing the query that was actually run, you may find problems in the query that results from your code. For instance, the query may not return any MySQL errors, but it could run a query that is different than you expect it to be. With this debug code, you'll at least be able to see what it is and continue on the wonderful debug trail! Furthermore, you may want to explore more of the "Class Variables" (Codex Ref) for $wpdb as they may help further troubleshoot your issue.

link|improve this answer
feedback

A zero response means zero rows affected, which is different from an error.

Its hard to say without looking at your query why no rows are being updated. One debug tool you can try is setting "SAVEQUERIES" to true in your wp-config.php file.

Then after your query runs, try var_dumping $wpdb->queries.

link|improve this answer
feedback

$wpdb->show_errors() shows errors "automagically" ... if WP_DEBUG is set to true.

link|improve this answer
feedback
$wpdb->show_errors();
$wpdb->print_error();
link|improve this answer
feedback

Try this before your query:

$wpdb->show_errors = TRUE;
$wpdb->suppress_errors = FALSE;

or perhaps this after your query:

if ($wpdb->last_error) {
  die('error=' . var_dump($wpdb->last_query) . ',' . var_dump($wpdb->error));
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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