我的 PDO 声明不起作用 [英] My PDO Statement doesn't work

查看:31
本文介绍了我的 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 实例变量的名称)或者 - 更好地 - 将此参数添加为 连接选项.之后,所有数据库错误都将转换为 PDO 异常,如果不理会这些异常,就会像常规 PHP 错误一样.

(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.问题点就在错误消息中引用的查询部分之前.

您还必须信任错误消息.如果它说标记的数量与绑定变量的数量不匹配,那么就是.不存在的表或列也是如此.给定选择,无论是你自己的错误还是错误信息是错误的,始终坚持前者.再次听起来居高临下,但这个网站上的数百个问题证明这个建议非常有用.

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 a 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.

    附言
    有时没有错误,但也没有结果.那么这意味着,没有符合您条件的数据.所以你必须承认这个事实,即使你可以发誓数据和标准都没有问题.他们不是.你必须再次检查它们.我有简短的回答可以帮助您查明匹配问题,使用 PDO 在数据库中匹配行有问题.只需按照此说明和链接的教程一步一步地进行操作,您的问题就会得到解决,或者为 Stack Overflow 提供一个可回答的问题.

    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 short answer that would help you to pinpoint the matching issue, Having issue with matching rows in the database using PDO. Just follow this instruction, and the linked tutorial step by step and either have your problem solved or have an answerable question for Stack Overflow.

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

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