Read our latest blog posts, learn something new, find answers, and stay up to date.

Debugging with WPDB

One of the best things about developing with WordPress is the WordPress Codex. The amount of documentation and examples makes development and debugging much more enjoyable compared to working with other code.

If you do custom WordPress development or even plugin and theme development and are not familiar with the following options available with WPDB, take a few minutes to read more about them and then test them out. These few commands have saved hours of frustration for us when debugging custom development.

You can turn error echoing on and off with the show_errors and hide_errors, respectively.

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->hide_errors(); ?> 

You can also print the error (if any) generated by the most recent query with print_error.

<?php $wpdb->print_error(); ?> 

Finally – you can also easily see your last query and last result using:

<?php $wpdb->last_query(); ?> 
<?php $wpdb->last_result(); ?> 

Further documentation from the WordPress Codex on these time saving helpers.

So – if you are ever unsure why a query is not working as you expect – simply add in the show error and print error code and you will instantly see exactly what is really going on.