问题描述
http://php.net/manual/en/pdo.error -handling.php
除了设置错误代码,PDO还将发出传统的E_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
除了设置错误代码之外,PDO还将引发PDOException并设置其属性以反映错误代码和错误信息.此设置在调试期间也很有用,因为它将在错误时有效地炸毁"脚本,非常快速地将手指指向代码中的潜在问题区域(请记住:如果异常导致脚本终止).
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).
异常模式也很有用,因为与在传统的PHP样式的警告中相比,与在静默模式下运行并显式检查每个数据库调用的返回值相比,您可以更加清晰地构造错误处理,并减少代码/嵌套. >
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错误处理的问题.
IMPORTANT: The question is not about problem with MySQL server gone, but about PDO error handling.
更新:在所有三种模式下触发警告:ERRMODE_SILENT,ERRMODE_WARNING,ERRMODE_EXCEPTION
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
推荐答案
我敢说这是一个错误.我找到了两个相关的票证:
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
无论如何,它们仍然处于打开状态,并且尚不清楚它们是否是有效的问题(尽管我怀疑是这样).这似乎不是设计决定,因为其他MySQL错误不会同时触发警告和异常:
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');
$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$connection->query('SELECT * FROM foo');
这篇关于PDO :: ERRMODE_EXCEPTION不会抑制警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!