How To Get Precise Error Messages For Issues Related To WordPress and MySQL 

If you like getting your hands dirty by playing with the internal coding of wordpress, I’m sure you have noticed that for the 4 possible errors related to WordPress and MySQL, the error message is still the same. But as a web developer, when your system in on local development phase, you would surely want to get precise error messages.

How To Pump Precise Error Message For Issues Related To WordPress and MySQL?

As of WordPress 2.9.x, database error messages seem to be handled by the method function dead_db() which is found in the php file: wp-includes/functions.php
You just have to insert the PHP method mysql_error() inside function dead_db() as shown below:

* Load custom DB error or display WordPress DB error.
* If a file exists in the wp-content directory named db-error.php, then it will
* be loaded instead of displaying the WordPress DB error. If it is not found,
* then the WordPress DB error will be displayed instead.
* The WordPress DB error sets the HTTP status header to 500 to try to prevent
* search engines from caching the message. Custom DB messages should do the
* same.
* This function was backported to the the WordPress 2.3.2, but originally was
* added in WordPress 2.5.0.
* @since 2.3.2
* @uses $wpdb
function dead_db()

echo(mysql_error()); ///============> INSERT OUR FUNCTION HERE

Now here are the more precise error messages for some possible errors associated with WordPress and MySQL:

  • When host name is wrongly set: php_network_getaddresses: getaddrinfo failed: Name or service not known
  • When database name is wrong: Unknown database ‘wordpressDB_NAMExxxx’
  • When database user name is wrong: Access denied for user ”@’localhost’ to database ‘wordpressDB_NAME’
  • When database password is wrong: Access denied for user ‘root’@’localhost’ (using password: YES)

A WorthWhile Note – Code it neatly!

Don’t just throw in the statement echo(mysql_error()); inside, what if you forget to remove it after your development is over? You don’t want your users to see those precise messages. A neat way to do this, is to declare a constant in your wp-config.php file, let’s say LOCAL_DEV

So, inside your wp-config.php file, you will have something like: define(‘LOCAL_DEV’, true);
And now let’s improve our injected line of code inside the functions.php file:

if( defined(‘LOCAL_DEV’) && LOCAL_DEV )
echo( mysql_error() );

Now this is much better ?! 😀
When ready for Production server, set back LOCAL_DEV to false: define(‘LOCAL_DEV’, false);

Do you have any other approach?

PS: If you are having headaches with your wordpress installation, you can hire my services to do your wordpress installation and updates!

Khayrattee Wasseem's Picture

About Khayrattee Wasseem

PHP enthusiast | The PHP Community Fanboi | Founder of PHP Usergroup for Mauritius (#PhpMauritius) | Aspiring Entrepreneur | Ex-country Manager of a global award winning Magento Agency | Passionate about all things Wordpress & Servers


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.