PHP 8.0: PDO: Default error mode set to exceptions
PHP's PDO
extension provides a consistent interface to access databases. it supports various database software, including MySQL, PostgreSQL and SQLite.
For each connection PDO makes to a database, it supports an attribute to specify how PDO should behave when an error occurs with the query, connection, or the database.
PDO::ATTR_ERRMODE
attribute controls how PDO should behave on errors.
PDO::ERRMODE_SILENT
(default): PDO will not raise warnings or throw exceptions in case of an error. It is up to the caller to inspect PDO::errorCode() and PDO::errorInfo() methods on both statement and PDO objects to determine errors.PDO::ERRMODE_WARNING
: Emits a warning (E_WARNING
) in addition to setting the error code and information.PDO::ERRMODE_EXCEPTION
: Throws aPDOException
in case of an error.
The error mode attribute can be set when instantiating the PDO object, or after the connection is established.
$pdo = new PDO($dsn, 'root', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$pdo = new PDO($dsn, 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Prior to PHP 8.0, the default error mode was to be silent (PDO::ERRMODE_SILENT
); PDO did not emit a warning or throw an exception if the database returned an error. Most libraries that deals with database often explicitly set the error mode to PDO::ERRMODE_EXCEPTION
because throwing an exception is desired as it minimizes the chances of overlooking database errors.
$pdo = new PDO('mysql:host=loc...', 'root', '');
$pdo->exec("DELETE FROM probably_missing_table WHERE id=42");
The snippet above, for example, tries to delete a record from a table that does not exist. It is desired that that the database error SQLSTATE[42S02]: Base table or view not found: 1146 Table 'phpwatch.myguests' doesn't exist
is brought as a PDOException
.
In PHP 8.0, the default PDO::ATTR_ERRMODE
attribute is set to PDO::ERRMODE_EXCEPTION
.
It is still allowed to explicitly set the attribute to PDO::ERRMODE_EXCEPTION
. Applications that already used the PDO::ERRMODE_EXCEPTION
error mode will not need to make any changes.
Backwards Compatibility Impact
Although not recommended, applications that relied on the PDO's silent behavior on errors can set PDO::ATTR_ERRMODE
attribute to PDO::ERRMODE_SILENT
. This will make PDO silently ignore all errors throughout the lifespan of that database connection.
$pdo = new PDO($dsn, 'root', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT
]);
To selectively ignore database errors, it is more appropriate to catch that error instead of setting the PDO::ATTR_ERRMODE
attribute throughout the connection.
- $pdo->exec("DELETE FROM probably_missing_table WHERE id=42");
+ try {
+ $pdo->exec("DELETE FROM probably_missing_table WHERE id=42");
+ }
+ catch (PDOException) {}
This snippet uses the PHP 8.0 feature to catch exceptions only by type.
Both approaches above are not ideal, as they ignore errors returned from the database, and ideally should be corrected in the query or the database structure.