PDO::ERRMODE_EXCEPTION doesn't suppress warning

I'd dare say it's a bug. I found two relevant tickets:

  • Bug #63812: PDO Triggers Warning(s) Regardless of Error Handling Strategy, filed on 2012 for PHP/5.3.19
  • Bug #74401: PDO trigger warning already set throw exception, filed on 2017 for PHP/7.0.17

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'

Tags:

Mysql

Php

Pdo