我的PDO声明无效 [英] My PDO Statement doesn't work

查看:68
本文介绍了我的PDO声明无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的PHP sql语句,并且在var dumping时返回false

This is my PHP sql statement and it's returning false while var dumping

$password_md5 = md5($_GET['password']);
$sql = $dbh->prepare('INSERT INTO users(full_name, e_mail, username, password, password_plain) VALUES (:fullname, :email, :username, :password, :password_plain)');
$result = $sql->execute(array(
                    ':fullname' => $_GET['fullname'], 
                    ':email' => $_GET['email'], 
                    ':username' => $_GET['username'],
                    ':password' => $password_md5,
                    ':password_plain' => $_GET['password']));

推荐答案

有时,您的PDO代码会产生类似Call to a member function execute()或类似错误.甚至没有任何错误,但查询不能完全一样.这意味着您的查询无法执行.

Sometimes your PDO code produces an error like Call to a member function execute() or similar. Or even without any error but the query doesn't work all the same. It means that your query failed to execute.

每次查询失败时,MySQL都会显示一条错误消息,说明原因.不幸的是,默认情况下,此类错误不会转移到PHP,而您所拥有的只是上面提到的静默或神秘的错误消息.因此,配置PHP和PDO报告MySQL错误非常重要.并且一旦收到错误消息,解决该问题将是轻而易举的事情.

Every time a query fails, MySQL has an error message that explains the reason. Unfortunately, by default such errors are not transferred to PHP, and all you have is a silence or a cryptic error message mentioned above. Hence it is very important to configure PHP and PDO to report you MySQL errors. And once you get the error message, it will be a no-brainer to fix the issue.

为了获得有关该问题的详细信息,请在连接后立即在代码中添加以下行

In order to get the detailed information about the problem, either put the following line in your code right after connect

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

(其中$dbh是您的PDO实例变量的名称)或-更好-将此参数添加为

(where $dbh is the name of your PDO instance variable) or - better - add this parameter as a connection option. After that all database errors will be translated into PDO exceptions which, if left alone, would act just as regular PHP errors.

在发生某些特定错误的情况下,不会引发异常的可能性很小.如果您的query()/prepare()execute()调用返回false,但没有例外,请像这样检查PDO::errorInfo()

There is a very small chance that in case of some specific error an exception won't be thrown. If your query()/prepare() or execute() call returns false but there is no exception, check the PDO::errorInfo() like this,

 trigger_error("PDO errorInfo: ".$dbh->errorInfo());

收到错误消息后,您必须阅读并理解它.听起来似乎太明显了,但是学习者经常忽略错误消息的意思.但大多数情况下,它可以很直接地解释问题:

After getting the error message, you have to read and comprehend it. It sounds too obvious, but learners often overlook the meaning of the error message. Yet most of time it explains the problem pretty straightforward:

  • 说,如果它说一个特定的表不存在,则必须检查拼写,错别字和字母大小写.另外,您还必须确保您的PHP脚本连接到正确的数据库
  • 或者,如果它说SQL语法有错误,那么您必须检查您的SQL.问题点就出现在错误消息中引用的查询部分的之前.

您还必须 trust 错误消息.如果它说令牌的数量与绑定变量的数量不匹配,那么它是 .缺少表或列的情况也是如此.如果有选择,无论是您自己的错误还是错误消息是错误的,请始终坚持前者.再次听起来是屈尊的,但是这个站点上的数百个问题证明了此建议非常有用.

You have to also trust the error message. If it says that number of tokens doesn't match the number of bound variables then it is so. Same goes for absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advice extremely useful.

请注意,为了查看PDO错误,您通常必须能够看到PHP错误.为此,您必须根据站点环境配置PHP:

Note that in order to see PDO errors, you have to be able to see PHP errors in general. To do so, you have to configure PHP depends on the site environment:

  • 开发服务器上,很容易在屏幕上出现错误,因此必须打开显示错误:

  • on a development server it is very handy to have errors right on the screen, for which displaying errors have to be turned on:

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

  • 在线网站上时,所有错误都必须记录下来,但永远不要显示给客户端.为此,请按以下方式配置PHP:

  • while on a live site, all errors have to be logged, but never shown to the client. For this, configure PHP this way:

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

  • 请注意,应始终将error_reporting设置为E_ALL.

    Note that error_reporting should be set to E_ALL all the time.

    还要注意,尽管存在常见的妄想,但错误报告不必使用try-catch . PHP将以一种更好的形式报告您的PDO错误.未捕获的异常对于开发非常有用,但是如果您要显示自定义的错误页面,仍然不要为此使用try catch,而只需设置自定义错误处理程序.简而言之,您不必将PDO错误视为特殊错误,而是将它们视为代码中的任何其他错误.

    Also note that despite the common delusion, no try-catch have to be used for the error reporting. PHP will report you PDO errors already, and in a way better form. An uncaught exception is very good for development, yet if you want to show a customized error page, still don't use try catch for this, but just set custom error handler. In a nutshell, you don't have to treat PDO errors as something special but regard them as any other error in your code.

    PS
    有时没有错误,但也没有结果.这意味着没有符合您条件的数据.因此,即使您可以保证数据和标准都正确,您也必须承认这一事实.他们不是.您必须再次检查它们.我有一篇文章可以对此提供帮助,如何调试与PDO的数据库交互.只需按照此说明逐步进行操作,就可以解决您的问题,或者对堆栈溢出有一个可以回答的问题.

    P.S.
    Sometimes there is no error but no results either. Then it means, there is no data to match your criteria. So you have to admit this fact, even if you can swear the data and the criteria are all right. They are not. You have to check them again. I've got an article that can help in this matter, How to debug database interaction with PDO. Just follow this instruction step by step and either have your problem solved or have an answerable question for Stack Overflow.

    这篇关于我的PDO声明无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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