PHP PDO。查询正确时的错误号“00000”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11813911/
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
PHP PDO. error number '00000' when query is correct
提问by Siamak A.Motlagh
I have the code below:
我有下面的代码:
$sql3 = "update news set date='$time' where id='2'";
$sql3 = $connect->exec($sql3);
if(!$sql3)
{
print_r($connect->errorInfo());
$error = $connect->errorInfo();
die ("Error: (".$error[0].':'.$error[1].') '.$error[2]);
}
When I run the script, sometimes I get error number '00000'. I mean it goes intro the IF. and it is all random. output (sometimes):
运行脚本时,有时会收到错误号“00000”。我的意思是它介绍了IF. 而且都是随机的。输出(有时):
Array ( [0] => 00000 [1] => [2] => )
What should I do to fix this problem ?
PS: The script executes correctly every time.
我应该怎么做才能解决这个问题?
PS:脚本每次都正确执行。
回答by SomeKittens
The PDO error code 00000means that everything works fine. The reason you're hitting the error-checking code is that $sql3is returning 0 (no rows were effected) and PHP evaluates that to false. Try explicitly checking for a return false;
PDO 错误代码00000意味着一切正常。您遇到错误检查代码的原因$sql3是返回 0(没有行受到影响)并且 PHP 将其评估为 false。尝试明确检查return false;
if($sql3 === false)
回答by Oussama
If exec does not update any row, it will return 0. that makes if(!$sql3) evaluate to false, you should do this instead :
如果 exec 不更新任何行,它将返回 0。这使得 if(!$sql3) 评估为 false,您应该这样做:
if($sql3 === false){
}
回答by Collector
In my experience badly formed queries (syntax errors) and failed queries (for example an INSERT that didn't insert anything) also may WRONGLY return error code 00000. You should go ahead and try to run the complete query on your SQL console and see why it failed. I really don't know why the proper error message isn't returned though. Here's a snippet of the code we use
根据我的经验,格式错误的查询(语法错误)和失败的查询(例如未插入任何内容的 INSERT)也可能错误地返回错误代码 00000。您应该继续尝试在 SQL 控制台上运行完整的查询并查看为什么失败。我真的不知道为什么没有返回正确的错误消息。这是我们使用的代码片段
$r = $pdo->prepare($sql);
if (!$r->execute($input_parameters)) { # query failed
if ($bLogFailures) {
error_log('query failed: ERROR['.$pdo->errorCode().':'.print_r($pdo->errorInfo(), true).'] QUERY['.$sql.']');
}
return false;
}
回答by Bryan Teague
The PDO::exec statement returns an integer to indicate the number of rows that were affected. So in your particular case, as the SomeKittens indicates, if 0 rows were affected, then your error code would be triggered.
PDO::exec 语句返回一个整数以指示受影响的行数。因此,在您的特定情况下,如 SomeKittens 所示,如果 0 行受到影响,则将触发您的错误代码。
However, if you are concerned as to whether your query worked, your better action may be to use PDO::query (in terms of your code ($returnObj = $connect->query($sql3) instead of PDO::exec.
但是,如果您担心您的查询是否有效,您更好的做法可能是使用 PDO::query(根据您的代码 ($returnObj = $connect->query($sql3) 而不是 PDO::exec。
The $returnObj can then be checked to see if there was an error in the SQL execution, and you can then troubleshoot your SQL query because it will tell you what the error was and where it was located.
然后可以检查 $returnObj 以查看 SQL 执行中是否存在错误,然后您可以对 SQL 查询进行故障排除,因为它会告诉您错误是什么以及错误所在的位置。
Your best bet to do this would be:
这样做的最佳选择是:
//set PDO to throw an error so you can wrap the query in a try / catch block.
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql3 = "update news set date='$time' where id='2'";
try {
$returnObj = $connect->query($sql3);
} catch (PDOException $e) {
print_r($returnOjb->errorInfo());
$error = $returnObj->errorInfo();
die ("Error: (".$error[0].':'.$error[1].') '.$error[2]);
}
回答by s.naseri
00000means, it works fine. you should change your if to this: $sql3 === false.
00000意味着,它工作正常。你应该改变你的,如果这样:$sql3 === false。
回答by user4065635
I just got similar situation in my php project - it occured that PDO Exception with error code '00000' took place when I tried to insert row with a field set to NULL while column defining the field in the database was of type ENUM('0', '1')and restriction NOT NULL.
After modifying PHP script to place '0' instead of NULL, the error perished.
我刚刚在我的 php 项目中遇到了类似的情况 - 当我尝试插入字段设置为 NULL 的行而在数据库中定义字段的列的类型ENUM('0', '1')和限制为NOT NULL时,发生了错误代码为“00000”的 PDO 异常. 修改 PHP 脚本以放置 '0' 而不是 后NULL,错误消失了。
Further coding brought more light into the situation - I was performing more than one PDO statments within one DB transaction but checking errors (in Exception handling block) basing only on the first PDO statement executed while real error occured int the third PDO statement.
进一步的编码使情况更加明朗——我在一个 DB 事务中执行了多个 PDO 语句,但检查错误(在异常处理块中)仅基于执行的第一个 PDO 语句,而真正的错误发生在第三个 PDO 语句中。
回答by bantya
I had the same problem. It also tortured me a lot, but finally figured it out.
我有同样的问题。它也折磨了我很多,但终于想通了。
Suppose you have 7 columnsin your table.
假设您的表中有7 列。
You are inserting data into 4of them.
您正在向其中 4 个中插入数据。
If for remaining 3 columnsthe default value is not set (say NULL for alpha-numeric columns, CURRENT_TIMESTAMP for date-time related columns etc.) then the above stated problem occurs.
如果对于剩余的3 列未设置默认值(例如NULL for alpha-numeric columns, CURRENT_TIMESTAMP for date-time related columns etc.),则会出现上述问题。
If you are inserting data into all of those 7 columnsor at leastin those columns for which default value is not set, you wont get any error and data will get inserted.
如果您将数据插入所有这 7 列或至少在未设置默认值的那些列中,您将不会收到任何错误并且数据将被插入。

