Skip to content

Commit

Permalink
Code Modernization: handle mysqli_ping() deprecation in wpdb::check_c…
Browse files Browse the repository at this point in the history
…onnection().

The `mysqli_ping()` function is deprecated as of PHP 8.4, though, in reality, the function wasn't working according to spec anymore since PHP 8.2 when the `libmysql` driver was dropped in favour of `libmysqlnd`, which was already the default (and recommended) driver since PHP 5.4.

The `mysqli_ping()` method was also not really correctly named as its functionality was to reconnect to the database, not just ping.

The alternative is to "manually" ping the database by sending a `DO 1` query (the cheapest possible SQL query).

Adding a PHP version based toggle was considered, but as mentioned above, the default driver has been `libmysqlnd` since PHP 5.4 and in that case, the function never worked anyway, so in reality `mysqli_ping()` was only really functional for the odd custom PHP compilation where `mysqli` was build against `libmysql` AND `reconnect` was not disabled.

With this in mind, this change replaces the call to `mysqli_ping()` with the `DO 1` query completely. If that query succeeds, it concludes the database connection is still alive. This solution should be the most stable as it will work for both PHP 7.2 <= 8.1, independently of which driver `mysqli` was compiled with, as well as for PHP 8.2+.

Note: It could also be considered to remove the function call to `mysqli_ping()` completely and rely on standard error handling in case the connection would have dropped, as after all, the fact that the connection existed at the moment the "ping" happened, is no guarantee that the connection will still exist when the next query is send.... this approach was not chosen so as WP has custom error handling and does not use the PHP native mysqli exceptions for this, which would make implementing this more awkward.

Includes a test to verify that the connection check works when there is a valid connection (this was previously not covered by tests).

Refs:
* https://wiki.php.net/rfc/deprecations_php_8_4#mysqli_ping_and_mysqliping
* php/php-src#11912 (comment)
* https://stackoverflow.com/questions/2546868/cheapest-way-to-determine-if-a-mysql-connection-is-still-alive/2546922#2546922
* php/php-src#11945
* https://wiki.php.net/rfc/mysqli_support_for_libmysql
* https://www.php.net/mysqli_ping

Follow-up to [56475], [27250], [27075].

Props jrf, hellofromTonya.
See #62061.

git-svn-id: https://develop.svn.wordpress.org/trunk@59069 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 19, 2024
1 parent de346c6 commit 1e60814
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/wp-includes/class-wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,8 @@ public function parse_db_host( $host ) {
* @return bool|void True if the connection is up.
*/
public function check_connection( $allow_bail = true ) {
if ( ! empty( $this->dbh ) && mysqli_ping( $this->dbh ) ) {
// Check if the connection is alive.
if ( ! empty( $this->dbh ) && mysqli_query( $this->dbh, 'DO 1' ) !== false ) {
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/tests/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2455,4 +2455,15 @@ public function test_use_mysqli_property_access() {

$this->assertTrue( $wpdb->use_mysqli );
}

/**
* Verify "pinging" the database works cross-version PHP.
*
* @ticket 62061
*/
public function test_check_connection_returns_true_when_there_is_a_connection() {
global $wpdb;

$this->assertTrue( $wpdb->check_connection( false ) );
}
}

0 comments on commit 1e60814

Please sign in to comment.