Reputation: 385
http://php.net/manual/en/pdo.error-handling.php
PDO::ERRMODE_WARNING
In addition to setting the error code, PDO will emit a traditional E_WARNING message. This setting is useful during debugging/testing, if you just want to see what problems occurred without interrupting the flow of the application.
PDO::ERRMODE_EXCEPTION
In addition to setting the error code, PDO will throw a PDOException and set its properties to reflect the error code and error information. This setting is also useful during debugging, as it will effectively "blow up" the script at the point of the error, very quickly pointing a finger at potential problem areas in your code (remember: transactions are automatically rolled back if the exception causes the script to terminate).
Exception mode is also useful because you can structure your error handling more clearly than with traditional PHP-style warnings, and with less code/nesting than by running in silent mode and explicitly checking the return value of each database call.
However, the code:
$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '***');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->query('SET wait_timeout=1;');
sleep(2);
try {
$connection->query('SELECT 1;');
} catch (\Exception $e) {
echo sprintf('Caught %s exception: %s', get_class($e), $e->getMessage()) . PHP_EOL;
}
triggers warnings:
PHP Warning: PDO::query(): MySQL server has gone away in pdo.php on line 13
PHP Warning: PDO::query(): Error reading result set's header in pdo.php on line 13
Caught PDOException exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
IMPORTANT: The question is not about problem with MySQL server gone, but about PDO error handling.
UPDATE: Warnings triggered in all three modes: ERRMODE_SILENT, ERRMODE_WARNING, ERRMODE_EXCEPTION
PHP 7.2.1 (cli) (built: Jan 5 2018 17:34:14) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
Upvotes: 8
Views: 3499
Reputation: 146490
I'd dare say it's a bug. I found two relevant tickets:
In any case, they're still open and it isn't entirely clear whether they're valid issues (though I suspect they are). It doesn't seem to be a design decision because other MySQL errors do not trigger both, warning and exception:
$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]);
$connection->query('SELECT * FROM foo');
Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.foo' doesn't exist
$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$connection->query('SELECT * FROM foo');
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.foo' doesn't exist'
Upvotes: 4
Reputation: 601
The purpose of PDO::ERRMODE_EXCEPTION
is not to prevent php warnings, notices or similar things.
As you can read in the documentation you have already provided by yourself it will raise an exception instead of writing a mysql error silently into the result.
So instead of you manually checking if the result has an error (mysql has gone away, you have a syntax error..., etc.) on the result, PDO will directly throw an exception which you can catch.
And PDO::ERRMODE_EXCEPTION
does exactly that as your code proves.
You have to differ between php errors, warnings etc which will still be shown and mysql errors which will be converted into an exception instead of being saved in the result.
Upvotes: 0