php PDO 错误信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3999850/
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 error message?
提问by JD Isaacks
Here is a snippet of my code:
这是我的代码片段:
$qry = '
INSERT INTO non-existant-table (id, score)
SELECT id, 40
FROM another-non-existant-table
WHERE description LIKE "%:search_string%"
AND available = "yes"
ON DUPLICATE KEY UPDATE score = score + 40
';
$sth = $this->pdo->prepare($qry);
$sth->execute($data);
print_r($this->pdo->errorInfo());
This should give me an error because the tables don't even exist. All I get however is this:
这应该给我一个错误,因为这些表甚至不存在。然而,我得到的是:
Array ( [0] => 00000 )
数组 ( [0] => 00000 )
How can I get a better description of the error so I can debug the issue?
如何更好地描述错误以便调试问题?
回答by Alan Geleynse
Try this instead:
试试这个:
print_r($sth->errorInfo());
Add this before your prepare:
在准备之前添加:
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
This will change the PDO error reporting type and cause it to emit a warning whenever there is a PDO error. It should help you track it down, although your errorInfo should have bet set.
这将更改 PDO 错误报告类型并使其在出现 PDO 错误时发出警告。它应该可以帮助你追踪它,虽然你的 errorInfo 应该已经下注了。
回答by lumonald
Old thread, but maybe my answer will help someone. I resolved by executing the query first, then setting an errors variable, then checking if that errors variable array is empty. see simplified example:
旧线程,但也许我的回答会对某人有所帮助。我通过首先执行查询,然后设置一个错误变量,然后检查该错误变量数组是否为空来解决。见简化示例:
$field1 = 'foo';
$field2 = 'bar';
$insert_QUERY = $db->prepare("INSERT INTO table bogus(field1, field2) VALUES (:field1, :field2)");
$insert_QUERY->bindParam(':field1', $field1);
$insert_QUERY->bindParam(':field2', $field2);
$insert_QUERY->execute();
$databaseErrors = $insert_QUERY->errorInfo();
if( !empty($databaseErrors) ){
$errorInfo = print_r($databaseErrors, true); # true flag returns val rather than print
$errorLogMsg = "error info: $errorInfo"; # do what you wish with this var, write to log file etc...
/*
$errorLogMsg will return something like:
error info:
Array(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table bogus(field1, field2) VALUES ('bar', NULL)' at line 1
)
*/
} else {
# no SQL errors.
}
回答by Vikram
Maybe this post is too old but it may help as a suggestion for someone looking around on this : Instead of using:
也许这篇文章太旧了,但它可能有助于为环顾四周的人提供建议:而不是使用:
print_r($this->pdo->errorInfo());
Use PHP implode() function:
使用 PHP implode() 函数:
echo 'Error occurred:'.implode(":",$this->pdo->errorInfo());
This should print the error code, detailed error information etc. that you would usually get if you were using some SQL User interface.
这应该打印错误代码、详细的错误信息等,如果您使用某些 SQL 用户界面,通常会得到这些信息。
Hope it helps
希望能帮助到你
回答by thetaiko
From the manual:
从手册:
If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).
如果数据库服务器成功准备了语句,PDO::prepare() 将返回一个 PDOStatement 对象。如果数据库服务器无法成功准备语句,PDO::prepare() 将返回 FALSE 或发出 PDOException(取决于错误处理)。
The prepare statement likely caused an error because the db would be unable to prepare the statement. Try testing for an error immediately after you prepare your query and before you execute it.
准备语句可能会导致错误,因为数据库将无法准备该语句。在准备好查询之后和执行之前,立即尝试测试错误。
$qry = '
INSERT INTO non-existant-table (id, score)
SELECT id, 40
FROM another-non-existant-table
WHERE description LIKE "%:search_string%"
AND available = "yes"
ON DUPLICATE KEY UPDATE score = score + 40
';
$sth = $this->pdo->prepare($qry);
print_r($this->pdo->errorInfo());