参考资料——关于 PDO 的常见问题 [英] Reference — frequently asked questions about PDO

查看:30
本文介绍了参考资料——关于 PDO 的常见问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于 PHP 数据对象的常见问题列表

This is a list of frequently asked questions regarding PHP Data Objects

由于 PDO 具有一些普通 PHP 用户不知道的功能,因此关于 PDO 中准备好的语句和错误处理的问题非常频繁.所以,这只是一个可以找到所有这些的地方.

As PDO has some features unknown to a regular PHP user, questions regarding prepared statements and error handling in PDO are quite frequent. So, this is just a place where all them can be found.

如果您的问题已通过此列表投票通过,请在下方找到您的问题并将修复应用到您的代码.简要地看看其他问题也是一个好主意,让自己为其他常见的陷阱做好准备.

If your question has been closevoted with this list, please find your question below and apply the fix to your code. It is also a good idea to take a brief look to other questions, to make yourself prepared for other common pitfalls.

另见

推荐答案

PDO 查询失败,但我看不到任何错误.如何从 PDO 获取错误消息?

为了能够看到数据库错误,必须设置PDO errmode 例外.异常在很多方面都比常规错误要好:它们总是包含堆栈跟踪,可以使用 try..catch 捕获它们或使用专用错误处理程序进行处理.即使未经处理,它们也会作为常规 PHP 错误提供所有重要信息,遵循站点范围的错误报告设置.

PDO query fails but I can't see any errors. How to get an error message from PDO?

To be able to see database errors, one have to set PDO errmode to exceptions. Exceptions are better than regular errors in many ways: they always contains a stack trace, they can be caught using try..catch or handled using dedicated error handler. And even unhandled, they act as regular PHP errors providing all the important information, following site-wide error reporting settings.

请注意,将此模式设置为连接选项将使 PDO 在连接错误时也抛出异常,这一点非常重要.
因此,以下是正确创建 PDO 连接的示例:

Note that setting this mode as a connection option will let PDO throw exceptions on connection errors too, which is very important.
So, here is an example for creating a PDO connection right way:

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    // other options 
);
$pdo = new PDO($dsn, $user, $pass, $opt);

通过这种方式连接,您将始终收到有关查询执行期间发生的所有数据库错误的通知.请注意,您必须能够看到一般的 PHP 错误.在实时站点上,您必须查看错误日志,因此设置必须

Connecting this way, you will be always notified of all database errors, occurred during query execution. Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

在本地开发服务器上,在屏幕上出错是可以的:

while on a local development server it's ok to make errors on screen:

error_reporting(E_ALL);
ini_set('display_errors',1);

当然,您永远不应该在 PDO 语句前使用错误抑制运算符 (@).

and of course you should never ever use error suppression operator (@) in front of your PDO statements.

另外,由于很多不好的例子告诉你将每个 PDO 语句都包装到 try..catch 块中,我必须做一个明确的注意:

Also, due to many bad examples telling you to wrap every PDO statement into try..catch block, I have to make a distinct note:

不要仅使用 try..catch 运算符来回显错误消息. 未捕获异常已经非常适合此目的,因为它的行为方式与其他 PHP 错误相同 - 所以,您可以使用站点范围的设置定义行为 - 因此,您将获得没有此无用代码的错误消息. 虽然无条件回显的错误消息可能会向潜在攻击者透露一些敏感信息,但会造成混淆一个诚实的访客.

DO NOT use try..catch operator just to echo an error message. Uncaught exception is already excellent for this purpose, as it will act just the same way as other PHP errors - so, you can define the behavior using site-wide settings - so, you will have your error message without this useless code. While unconditionally echoed error message may reveal some sensitive information to a potential attacker, yet confuse a honest visitor.

  • 可以稍后添加自定义异常处理程序,但不是必需的.特别是对于新用户,建议使用未处理的异常,因为它们非常有用、有用且安全.
  • 仅当您要处理错误本身时才使用 try..catch - 例如,回滚事务.
  • A custom exception handler could be added later, but not required. Especially for new users, it is recommended to use unhandled exceptions, as they are extremely informative, helpful and secure.
  • Use try..catch only if you are going to handle the error itself - say, to rollback a transaction.

这篇关于参考资料——关于 PDO 的常见问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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