6

I am developing a wordpress site and I would like to be able to log and handle mysql errors from wordpress core.

My site has a mix of wordpress pages and posts + a few php pages that run under the wordpress engine. I configured the php.ini to prepend a php file to all php scripts with the error handling functions. They are working fine to catch php errors both in the wordpress core and in my own php scripts.

I also catch all the MySQL errors in my own php scripts. For example, failure to connect to a database, etc.

I would like to be able to also catch, log and process eventual MySQL errors from the wordpress core application.

Any suggestions?

Thanks.

I was reading through the codex and I couldn't find an answer

1
  • What are you doing that cannot be done with $wpdb or WP_Query? Commented Jun 13, 2015 at 2:28

3 Answers 3

8

You should be using the wpdb class for all your own queries. All core queries also use wpdb. See wpdb Show and Hide SQL Errors

<?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(); ?>

Also see SAVEQUERIES constant for wp-config.php:

define('SAVEQUERIES', true);

usage example:

<?php
if (current_user_can('administrator')){
    global $wpdb;
    echo "<pre>";
    print_r($wpdb->queries);
    echo "</pre>";
}
?>

There are also a number of helpful debugging plugins, like Debug Bar & Console. Search the WordPress plugin repository for this and other debugging related plugins.

1
  • no error_log? Commented Jun 13, 2015 at 2:29
0

One way to do this:

  1. Create a file called db.php in wp-content. Wordpress will read this file immediately before instantiating the $wpdb object.
  2. Create a class which extends wpdb, and overwrite the print_errors method to do whatever you want.
  3. In the end of that file, instantiate your class, setting it the variable $wpdb (eg., $wpdb = new wpdb2(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);)
  4. Wordpress will now use your $wpdb instead.
0

try with

global $wpdb;
echo $wpdb->last_query;

as i am working with ajax, this is directly in string form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.