PDO::ERRMODE_EXCEPTION vs PDO::ERRMODE_SILENT

I thought that might be happening, but wasn’t sure because I am not sure about manipulating PDO very well (working on it).

I found that no code I tried prevents PDO from throwing an error if an error occurs during the constructor.
Even this, passed in, doesn’t work:

$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));

But PDO::ERRMODE_SILENT does work if the PDO object is constructed successfully. It does not throw exceptions for bad queries and errors are not logged to php_error_log.

The manual does mention this, but I missed it because it’s actually hidden between two examples, instead of at the top where it should be.

PDO::__construct() will always throw a PDOException if the connection fails regardless of which PDO::ATTR_ERRMODE is currently set. Uncaught Exceptions are fatal.

Thanks for the help, both of you.

1 Like