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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Show errors:
$wpdb->show_errors = trueshows errors automatically, ifWP_DEBUGis set totrue.-
$wpdb->suppress_errors = falsestops suppressing errors. -
Multisite need special treatment
// Show errors in Multisite: global $wpdb, $blog_id; // There's no is_multisite(), so we need to check the ID // This means, that we can't debug the blog with the ID 1 as MU-blog by default // Check if we are on Blog ID#1 and if not, check the defines and add error handling if ( 1 !== $blog_id ) ! defined( 'DIEONDBERROR' ) AND define( 'DIEONDBERROR', true );
Output handling
The $wpdb->update() method has three different outputs. To check against it, you must save the result i a var: $result = $wpdb->update( /* ... */ );.
The handle those scenarios:
false === $result: Fail0 === $result: Success, but no update0 < $result: Success
Class output
$wpdb->last_errorwill show you the last error, if you got one.$wpdb->last_querywill assist you with showing the last query (where the error occurred). It is basically the same asarray_pop( $wpbd->queries );.
Important (security) Note
Please do NOT add this codes on live site. Especially not if caching plugins are involved. This may expose important DB-related data to visitors!
If you can’t do otherwise: always wrap your code in conditional statements to prevent public facing debug output!
// Example
function debug_query( $result, $data )
{
global $current_user;
get_currentuserinfo();
if ( current_user_can( 'manage_options' ) )
{
global $wpdb, $blog_id;
1 !== $blog_id
AND ! defined( 'DIEONDBERROR' )
AND define( 'DIEONDBERROR', true );
$wpdb->show_errors = true;
$wpdb->suppress_errors = false;
$output = '<pre style="white-space:pre-line;">';
$output .= 'Last Error: ';
$output .= var_export( $wpdb->last_error, true );
$output .= "nnLast Query: ";
$output .= var_export( $wpdb->last_query, true );
if ( false === $result )
{
$result = new WP_Error( 'query_failed', 'No update.', $data );
}
elseif ( 0 === $result )
{
$result = new WP_Error( 'update_failed', 'Updated zero rows.', $data );
}
elseif ( 0 < $result )
{
$result = 'Success';
}
$output .= '</pre>';
// Only abort, if we got an error
is_wp_error( $result )
AND exit( $output.$result->get_error_message() );
}
}
Exposing the $wpdb object might also expose your database username and password!
Method 3
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.
Method 4
$wpdb->show_errors(); $wpdb->print_error();
Method 5
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));
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0