pdo我应该尝试/捕获每个查询吗? [英] pdo should i try/catch every query?

查看:57
本文介绍了pdo我应该尝试/捕获每个查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上没有错误,但是我需要将mysqli调用更改为pdo,因为我需要一个预准备的语句调用,其中我不知道需要传递多少参数,而且看来我可以用pdo传递参数数组.

i don't have an error on my page, but i need to change my mysqli calls to pdo because i need an prepared statement call where i don't know how much parameters i need to pass and it seems like i can pass an array of parameters with pdo.

我在此页面上阅读了 :

警告!如果您的应用程序没有捕获到PDO构造函数引发的异常,则zend引擎采取的默认操作是终止脚本并显示向后跟踪.回溯跟踪可能会显示完整的数据库连接详细信息,包括用户名和密码.您有责任通过显式(通过catch语句)或通过set_exception_handler()隐式捕获此异常.

Warning! If your application does not catch the exception thrown from the PDO constructor, the default action taken by the zend engine is to terminate the script and display a back trace. This back trace will likely reveal the full database connection details, including the username and password. It is your responsibility to catch this exception, either explicitly (via a catch statement) or implicitly via set_exception_handler().

我以前从未使用过pdo,并且一些教程没有指出我可以不知道就放弃我的用户名/密码的事实.

i never used pdo before and some tutorials don't point out the fact that i could give away my username/password without knowing.

我应该将每个查询还是仅将new PDO();放在try/catch中?这是好习惯吗?我应该在try/catch上使用set_exception_handler()吗?

should i put every query or just the new PDO(); in a try/catch? is it good practice? should i use the set_exception_handler() over try/catch?

推荐答案

TL; DR:

大多数查询不会产生异常.

Most of your queries will not produce exceptions.

项目在开发中时,未捕获的异常将使您可以发现SQL代码中的错误.而且,当项目投入生产时,只有您看到的例外才是预期的例外.像唯一键违例"一样,有一些外键限制.

While your project is in development, the uncaught exceptions will let you find errors in your SQL code. And when project goes in production, only exceptions that you see should be expected ones. Like "unique key violation", some foreign key restrictions.

基本上,在生产代码中,查询周围应该只有try-catch个块,其中异常是预期逻辑的一部分.

Basically, in production code, you should have only try-catch blocks surrounding queries, where exception is part of the expected logic.

这篇关于pdo我应该尝试/捕获每个查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆