参考-有关PDO的常见问题 [英] Reference — frequently asked questions about PDO

查看:76
本文介绍了参考-有关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 query fails but I can't see any errors. How to get an error message from PDO?
  • How can I use prepared statements with LIKE operator?
  • How can I create a prepared statement for IN () operator?
  • Can I use a PDO prepared statement to bind an identifier (a table or field name) or a syntax keyword?
  • PDO prepared statement causes an error in LIMIT statement

另请参见

  • Reference - What does this symbol mean in PHP?
  • Reference - What does this error mean in PHP?
  • Reference - What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

推荐答案

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

要查看数据库错误,必须设置 PDO错误模式例外.在许多方面,异常比常规错误要好:它们总是包含堆栈跟踪,可以使用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天全站免登陆