PHP 8.1 – Default MySQLi Error Mode

PHP 8.1 – Default MySQLi Error Mode

database icon with error icon

The default error mode for mysqli calls is now changed from silent mode to Exception mode. This doesn’t affect PDO object since PHP Data Object is already throwing Exception by default.

This change is setting the default mysqli error reporting mode to exception mode. The new setting would be the same as manually setting the error mode with the following line of code:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This change will save developers countless hours of debugging. It will also bring the behavior of this extension in line with PDO and the rest of PHP.

mysqli_report(MYSQLI_REPORT_OFF);
$mysqli = new mysqli("localhost", "user", "password", "database");
 
$result = $mysqli->query('SELECT * FROM invalid_table');

if (false === $result) {
	my_error_handling_function($mysqli->error);
}

MYSQLI_REPORT_ERROR will tell mysqli to throw a warning error message when an error is encountered. The code is not stopped and continues to execute.

MYSQLI_REPORT_STRICT will tell mysqli to throw mysqli_sql_exception instead whenever it would throw a warning. It doesn’t control whether errors are reported, but instead controls how they are reported. The only code that throws a warning without MYSQLI_REPORT_ERROR is the connection code.

More about PHP 8.1

Read about all PHP 8.1 features and changes in here.

Leave a Comment