Does PDO fetch() throw an exception on failure?(PDO fetch() 是否会在失败时抛出异常?)
问题描述
方法 PDOStatement::fetch() 是否会抛出异常失败,如果PDO错误报告系统设置为抛出异常?例如.如果我设置:
Does the method PDOStatement::fetch() throw an exception on failure, if the PDO error reporting system is set to throw exceptions? E.g. if I set:
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
你知道这样的案例吗?
非常感谢.
PdoStatement::fetch 方法确实会在失败时抛出异常,而不是 FALSE.我的回答中证明了这种情况:
The method PdoStatement::fetch throws indeed exceptions on failure, instead of FALSE. Such a case is demonstrated in my answer:
- 模拟 PDO 获取失败情况
总结:
PDOStatement::fetch如果没有找到记录,则返回FALSE.PDOStatement::fetch失败时抛出异常.
PDOStatement::fetchreturnsFALSEif no records are found.PDOStatement::fetchthrows exceptions on failure.
推荐答案
用户 here 声称 fetch() 引发了异常.我会非常小心地假设它不会或不会抛出异常,因为它们通常在您准备或绑定时抛出.这是将调用放在 try 块中的一个很好的理由.所以要回答这个问题,在极不可能发生的失败事件中,是的 fetch() 应该抛出一个异常,在这种情况下它确实发生了.现在看看是否还有其他情况会很有趣.
The user here claims that fetch() threw an exception. I'd be very careful in assuming that it doesn't or won't throw an exception just because they are typically thrown when you prepare or bind. This is a very good reason to put the call inside a try block. So to answer the question, in the highly unlikely event of failure, yes fetch() should throw an exception and in that one case it did. Now it will be interesting to see if there are other cases as well.
这篇关于PDO fetch() 是否会在失败时抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO fetch() 是否会在失败时抛出异常?
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
