Make WordPress Core

Changeset 51582


Ignore:
Timestamp:
08/08/2021 02:08:15 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Set the MySQLi error reporting off for PHP 8.1.

Prior to PHP 8.1, the default error handling mode was MYSQLI_REPORT_OFF. An error in the extension, database, query, or the database connection returned false and emitted a PHP warning:

$mysqli = new mysqli("localhost", "non-existing-user", "", "");

Warning: mysqli::__construct(): (HY000/2002): No connection could be made because the target machine actively refused it in ... on line ...

From PHP 8.1 and later, the default error mode is set to MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT. An error in the extension, database, query, or the database connection throws an exception:

$mysqli = new mysqli("localhost", "non-existing-user", "", "");

Fatal error: Uncaught mysqli_sql_exception: Connection refused in ...:...

WordPress has its own error reporting and gracefully handles the database errors by inspecting the error codes. Setting the MySQLi error reporting to off avoids fatal errors due to uncaught exceptions and maintains the current behavior.

References:

Props ayeshrajans, jrf.
Fixes #52825.

File:
1 edited

Legend:

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

    r51477 r51582  
    16281628
    16291629        if ( $this->use_mysqli ) {
     1630            /*
     1631             * Set the MySQLi error reporting off because WordPress handles its own.
     1632             * This is due to the default value change from `MYSQLI_REPORT_OFF`
     1633             * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1.
     1634             */
     1635            mysqli_report( MYSQLI_REPORT_OFF );
     1636
    16301637            $this->dbh = mysqli_init();
    16311638
Note: See TracChangeset for help on using the changeset viewer.