php PDO SQL 状态为“00000”但仍然出错?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11519966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO SQL-state "00000" but still error?
提问by Chris
Can anybody explain why
任何人都可以解释为什么
$sql->execute($params);
returns FALSE, whereas
返回FALSE,而
print $pdo->errorCode();
print_r($pdo->errorInfo());
both return SQLSTATE00000, which means according to the documentation success? It is an INSERTand nothing is actually being inserted into the database... so, why do I get a success message from SQLSTATE?
两者都返回SQLSTATE00000,这意味着根据文档成功?它是一个INSERT并且实际上没有任何东西被插入到数据库中......那么,为什么我会收到来自 的成功消息SQLSTATE?
In case it helps, this is the code...
如果有帮助,这是代码...
$sql = $pdo->prepare("
INSERT INTO user (
username, fname, pass, salt, email,
loc_id_home, country_id_home, region_id_home,
cont_id_home, timestamp_reg, timestamp_upd, timestamp_lastonline,
online_status, gender, birthdate
)
VALUES (
:username,:fname,:pass,:random_salt,:email,
:loc_id_home,:country_id_home,:region_id_home,
:cont_id_home,'".time()."','".time()."','".time()."',
1,:gender,:birthdate)
");
$params=array(
':username'=>$username,
':fname'=>$fname,
':pass'=>$pass,
':random_salt'=>$random_salt,
':email'=>$email,
':loc_id_home'=>$loc_id_home,
':country_id_home'=>$country,
':region_id_home'=>$region,
':cont_id_home'=>$continent,
':gender'=>$gender,
':birthdate'=>$birthdate
);
$sql->execute($params);
print $pdo->errorCode();
print_r($pdo->errorInfo());
采纳答案by user1122069
It is because $pdo->errorInfo()refers to the last statement that was successfully executed. Since $sql->execute()returns false, then it cannot refer to that statement (either to nothing or to the query before).
这是因为$pdo->errorInfo()指的是最后一条成功执行的语句。由于$sql->execute()返回 false,则它不能引用该语句(要么什么也不引用,要么引用之前的查询)。
As to why $sql->execute()returns false, I don't know... either there is a problem with your $paramsarray or with your database connection.
至于为什么$sql->execute()返回false,我不知道......要么是你的$params数组有问题,要么是你的数据库连接有问题。
PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
PDO::errorCode — 获取与数据库句柄上的最后一个操作相关联的 SQLSTATE
Note: The PHP manual (http://php.net/manual/en/pdo.errorinfo.php) does not define exactly what "last operation on the database handle" means, but if there was an issue with binding parameters, that error would have occurred inside PDO and without any interaction with the database. It is safe to say that if $pdo->execute()returns true, that $pdo->errorInfo()is valid. If $pdo->execute()returns false, the behavior of $pdo->errorInfo()is not explicitly clear from the documentation. If I recall correctly from my experience, execute returns true, even if MySQL returned an error, returns falseif no operation was done. Since the documentation is not specific, it might be db driver specific.
注意:PHP 手册 ( http://php.net/manual/en/pdo.errorinfo.php) 没有准确定义“对数据库句柄的最后操作”的含义,但是如果绑定参数存在问题,那错误会发生在 PDO 内部并且与数据库没有任何交互。可以肯定地说,如果$pdo->execute()返回true,那$pdo->errorInfo()是有效的。如果$pdo->execute()返回false,$pdo->errorInfo()则文档中没有明确明确的行为。如果我从我的经验中没有记错的话,执行返回true,即使 MySQL 返回错误,false如果没有执行任何操作,也会返回。由于文档不是特定的,它可能是特定于数据库驱动程序的。
This answer reflects practical experience as of when it was written in September 2012. As a user has pointed out, the documentation does not explicitly reaffirm this interpretation. It also may only reflect the particular database driver implementation, but it should always be true that if $pdo->execute()returns true, that $pdo->errorInfo()is valid.
此答案反映了截至 2012 年 9 月撰写时的实践经验。正如用户所指出的,文档并未明确重申这种解释。它也可能只反映特定的数据库驱动程序实现,但如果$pdo->execute()返回true,$pdo->errorInfo()则它应该总是正确的,那就是有效的。
You might also want to set PDO::ERRMODE_EXCEPTIONin your connect sequence. Exception handling makes it unnecessary to check and query the error.
您可能还想在连接序列中设置PDO::ERRMODE_EXCEPTION。异常处理使得没有必要检查和查询错误。
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
回答by Renjith K N
I Faced the similar problem ,
我遇到了类似的问题,
This occurs manly due to error in query, try to run your query in php-myadminor any other query runner and confirmthat your query is workingfine.
Even if our query syntax is correct other simple errors like leaving null or not mentioan a column that set as not null in table structure will cause this error.(This was the errror made by me)
这主要是由于查询中的错误而发生的,请尝试在 php-myadmin或任何其他查询运行器中运行您的查询并确认您的查询工作正常。
即使我们的查询语法是正确的,其他简单的错误,例如保留空值或不提及在表结构中设置为非空值的列也会导致此错误。(这是我犯的错误)
As user1122069 explained the reason for $pdo->errorInfo() says nothing is wrong may be due to
正如 user1122069 解释 $pdo->errorInfo() 的原因说没有错可能是由于
$pdo->errorInfo() refers to the last statement that was successfully executed.
Since $sql->execute() returns false, then it cannot refer to that statement (either to nothing or to the query before)
$pdo->errorInfo() 指的是最后一条成功执行的语句。
由于 $sql->execute() 返回 false,因此它不能引用该语句(要么是空的,要么是之前的查询)
Hopes this helps :)
希望这有帮助:)
回答by Diego Saa
From the php manual:
从 php 手册:
PDO::ERR_NONE (string) Corresponds to SQLSTATE '00000', meaning that the SQL statement was successfully issued with no errors or warnings. This constant is for your convenience when checking PDO::errorCode() or PDOStatement::errorCode() to determine if an error occurred. You will usually know if this is the case by examining the return code from the method that raised the error condition anyway.
PDO::ERR_NONE (string) 对应于 SQLSTATE '00000',表示 SQL 语句成功发出,没有错误或警告。当检查 PDO::errorCode() 或 PDOStatement::errorCode() 以确定是否发生错误时,此常量是为了方便您。您通常会通过检查引发错误条件的方法的返回代码来了解是否是这种情况。
So it sounds like it did insert the record. Check the last record id in your table... maybe you just missed it?
所以听起来它确实插入了记录。检查表中的最后一个记录 ID...也许您只是错过了它?
回答by Matthew
I was getting this error at one time. I only got it on one server for all failures. A different server would report the error correctly for the same errors. That led me to believe it was a MySQL client configuration error. I never solved the specific error, but check your configurations.
我曾经遇到过这个错误。对于所有故障,我只在一台服务器上使用它。对于相同的错误,不同的服务器会正确报告错误。这让我相信这是 MySQL 客户端配置错误。我从未解决过特定错误,但请检查您的配置。
回答by Farhan
Try to check $sql by print_r() and copy your query then try resultant query in phpMyadmin. Hope will get the reason. There would be chance of irrelevant value.
尝试通过 print_r() 检查 $sql 并复制您的查询,然后在 phpMyadmin 中尝试结果查询。希望能得到原因。会有不相关的价值的机会。

