PHP 5.6 and MySQLi on Debian 8

We have recently finished upgrading all our user and service nodes to the latest Debian Linux operating system. This brings many bug fixes and performance improvements across the board, but we wanted to highlight a couple of things that relate to WordPress.

PHP 5.6

PHP 5.6 is now the default version provided by the official (and stable) Debian packages repository, which means all Pressjitsu nodes are now running version 5.6. This is a big performance improvement over 5.4 (the default version in Wheezy) as illustrated by this graph:

PHP 5.6 Performance
PHP 5.6 Performance

We’ve seen a 40-50% decrease in page load times across all user nodes on average. For requests served from cache (green) it didn’t have that much of an impact since they’re already extremely fast, but for things like the WordPress Dashboard it makes a huge difference.

MySQLi

The legacy MySQL extension has been deprecated in PHP 5.5 which is something to keep in mind when migrating from 5.4 to 5.6. WordPress core has full support for the replacement MySQLi extension and uses it in favor of MySQL functions in PHP 5.5+.

That said, even though the old mysql_* functions were deprecated, they weren’t removed from PHP and are still available even in version 5.6. This means that code relying on such functions will still work as expected, but there is one important issue when it comes to WordPress – there no longer is a default MySQL connection resource starting with version 5.5.

This means that even functions like mysql_real_escape_string will fail and return false instead of the expected escaped string, because there is no default MySQL connection required by the function. Here’s an excerpt of some code that triggered the MySQL warnings immediately after the 5.6 upgrade:

$user_id = $_GET['user_id'];
$wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_author = '" . mysql_real_escape_string( $user_id ) . "' AND post_status = 'publish';" );

The general rule of thumb is to never use database-specific functions such as mysql_* and mysqli_* when working with WordPress, and always use the wrapper $wpdb object instead. We were able to fix the above broken code by simply using $wpdb->prepare():

$user_id = $_GET['user_id'];
$wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_author = %d AND post_status = 'publish';", $user_id ) );

It is also a good idea to enable verbose PHP logging when upgrading from one version to another, which makes it easier to spot any breakage, but other than the MySQL extension problem, we haven’t found any major issues during the upgrade.

If you’re a Pressjitsu customer and noticed something out of the ordinary after the upgrade to PHP 5.6, please open a support request via the control panel and we’ll help you sort things out as soon as possible.