php - PDO::ERRMODE_EXCEPTION 不抑制警告

标签 php mysql pdo

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.

然而,代码:

$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;
}

触发警告:

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

重要:问题不是关于 MySQL 服务器消失的问题,而是关于 PDO 错误处理的问题。

更新:警告在所有三种模式下触发: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

最佳答案

我敢说这是一个错误。我找到了两张相关的票:

  • Bug #63812 : 无论错误处理策略如何,PDO 都会触发警告,2012 年针对 PHP/5.3.19 提交
  • Bug #74401 : PDO trigger warning already set throw exception, filed on 2017 for PHP/7.0.17

无论如何,它们仍然是开放的,并且不完全清楚它们是否是有效的问题(尽管我怀疑它们是)。这似乎不是设计决定,因为其他 MySQL 错误不会同时触发警告和异常:

$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'

关于php - PDO::ERRMODE_EXCEPTION 不抑制警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682621/

相关文章:

php - magento从1.4.0.1升级到1.4.2.0

php - 将表单 POST 提交到另一个页面,但在重定向之前进行验证,如果验证失败,则保留在同一页面上

php - 如何向bind_param添加特殊条件?

php - 构建我的 MySQL 搜索查询以与 PDO 一起使用

php - PDO 准备和执行查询总是返回错误

php - 为 PHP 设置浏览器

html - 让两个(最少)在 mysql 的一个查询中工作

java - @ManyToOne JPA spring mysql插入错误,列为空

php - 需要 MySQL 语句的帮助

php - PDO::PARAM_INT 是多余的吗?