From 25bc89f44a05f9ea92bd732721e7b95be56f1bed Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 8 Aug 2021 14:08:15 +0000 Subject: [PATCH] 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: * [https://php.watch/versions/8.1/mysqli-error-mode PHP 8.1: MySQLi: Default error mode set to exceptions] * [https://wiki.php.net/rfc/mysqli_default_errmode PHP RFC: Change Default mysqli Error Mode] Props ayeshrajans, jrf. Fixes #52825. git-svn-id: https://develop.svn.wordpress.org/trunk@51582 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 0d867868b8..04fa15c1ee 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -1627,6 +1627,13 @@ class wpdb { $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; if ( $this->use_mysqli ) { + /* + * Set the MySQLi error reporting off because WordPress handles its own. + * This is due to the default value change from `MYSQLI_REPORT_OFF` + * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1. + */ + mysqli_report( MYSQLI_REPORT_OFF ); + $this->dbh = mysqli_init(); $host = $this->dbhost;