DB driver: Error Mode


default error mode setting for DB driver:

const DB_ERROR_MODE	= 0;		// DB error mode: 0 - silent, 1 - warning, 2 - exception	

1. MySQLi

https://wiki.php.net/rfc/mysqli_default_errmode
https://www.php.net/manual/en/[...]iver.report-mode.php

As of PHP 8.1.0, the default setting is MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT. Previously, it was MYSQLI_REPORT_OFF.

In other words MYSQLI_REPORT_STRICT will cause a FATAL ERROR for a missing table, field or syntax error. Who wants this in an production environment by default - I don't get it.


Sets mysqli error reporting mode.

Name Description
MYSQLI_REPORT_OFF Turns reporting off
MYSQLI_REPORT_ERROR Report errors from mysqli function calls
MYSQLI_REPORT_STRICT Throw mysqli_sql_exception for errors instead of warnings
MYSQLI_REPORT_INDEX Report if no index or bad index was used in a query
MYSQLI_REPORT_ALL Set all options (report all)

Add option to turn reporting off by default.

<?php

$options = match (DB_ERROR_MODE)
{
    0    => MYSQLI_REPORT_OFF,
    1    => MYSQLI_REPORT_ERROR,
    2    => MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT,
};

mysqli_report($options);

Setting the MySQLi error reporting to off avoids fatal errors due to uncaught exceptions and maintains the current behavior.

2. PDO

https://wiki.php.net/rfc/pdo_default_errmode
https://www.php.net/manual/en/pdo.error-handling.php


PDO::ATTR_ERRMODE: This attribute is used for error reporting. It can have one of the following values.

Name Description
PDO::ERRMODE_SILENT If the ATTR_ERRMODE is not set in the code, ERRMODE_SILENT is the default value of ATTR_ERRMODE attribute. It sets error codes. In silent mode, if there is an error in SQL, PDO will throw no exceptions; PDO will issue no warnings; it will simply return false. Value of PDO::ERRMODE_SILENT is 0. The script will run without generating any error or warning.
PDO::ERRMODE_WARNING This value raises E_WARNING. In warning mode, if there is an error in SQL, PDO will issue warnings but script will continue running. Value of PDO::ERRMODE_WARNING is 1. The script will run with generating warning about the error.
PDO::ERRMODE_EXCEPTION This value throws exceptions. In exception mode, if there is an error in SQL, PDO will throw exceptions and script will stop running. Value of PDO::ERRMODE_EXCEPTION is 2. The script will stop executing generating the error which throws the exception.

<?php

$options = match (DB_ERROR_MODE)
{
    0    => [PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT],
    1    => [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING],
    2    => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
};

try
{
    $this->dblink = new PDO($dsn, $config->db_user, $config->db_password, $options);
}
catch (PDOException $e)
{
    die('PDO DSN Error: ' . $e->getMessage());
}