Temporary Workaround for mysql_query in WordPress in PHP 5.5+

This workaround can buy you some time if you’re working with an old WordPress site that relies on mysql_query.

I was recently asked to analyze an old WordPress site to determine what it would take to get it back in running order. Long story short, my advice was to basically rework the entire site in WordPress, scrapping the existing PHP MVC framework that was using WordPress only for its database and some front-end display.

But before I could even offer that advice (and see if the project would be worth my while to take on), I needed to access and view all the pages of the site—but many parts of the site were failing with the error message Access denied for user ''@'localhost' (using password: NO).

This usually points to a database connection error, but I could tell that overall WordPress was connecting just fine to the database, because many of the pages worked fine. Only certain parts of the site were failing, so it couldn’t be due to incorrect credentials.

By comparing the code used by the failing parts and the working parts, I narrowed down the problem to the use of mysql_query, an older method of querying the database that was deprecated in PHP 5.5. It was not actually removed from PHP until version 7.0, though, and the website in question is still running PHP 5.6. So what’s up?

Thanks to Craig Edmonds, I learned that WordPress stopped supporting the use of mysql_query with PHP 5.5, in an effort to encourage people to switch over to newer methods of accessing the database. But we’re in luck (for a short period of time)—you can force WordPress to use mysql_query by adding the following lines to your wp-config.php file:

// file: "wp-config.php"
#######################################
// Allow mysql_* on PHP 5.5+
// Usage: add this code to your wp-config.php file
#######################################
define("WP_USE_EXT_MYSQL", true);

Please note that this is only a stopgap measure to buy you some time! PHP 5.6 (and PHP 7.0) reach end of life on December 31, 2018 and you’ll want to be sure to upgrade to 7.2 as soon as possible. Once you do, this will no longer work. You’ll have to move to an alternative like PDO or mysqli.


© 2024 Jennifer Galas. All rights reserved.