Make WordPress Core

Changeset 59069


Ignore:
Timestamp:
09/19/2024 06:55:00 PM (9 months ago)
Author:
hellofromTonya
Message:

Code Modernization: handle mysqli_ping() deprecation in wpdb::check_connection().

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:

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

Props jrf, hellofromTonya.
See #62061.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wpdb.php

    r57926 r59069  
    21152115     */
    21162116    public function check_connection( $allow_bail = true ) {
    2117         if ( ! empty( $this->dbh ) && mysqli_ping( $this->dbh ) ) {
     2117        // Check if the connection is alive.
     2118        if ( ! empty( $this->dbh ) && mysqli_query( $this->dbh, 'DO 1' ) !== false ) {
    21182119            return true;
    21192120        }
  • trunk/tests/phpunit/tests/db.php

    r58164 r59069  
    24562456        $this->assertTrue( $wpdb->use_mysqli );
    24572457    }
     2458
     2459    /**
     2460     * Verify "pinging" the database works cross-version PHP.
     2461     *
     2462     * @ticket 62061
     2463     */
     2464    public function test_check_connection_returns_true_when_there_is_a_connection() {
     2465        global $wpdb;
     2466
     2467        $this->assertTrue( $wpdb->check_connection( false ) );
     2468    }
    24582469}
Note: See TracChangeset for help on using the changeset viewer.