此 PDO 准备好的语句返回 false 但不会引发错误
这段代码不会抛出错误,但查询失败,即 execute
方法返回 false。怎么可能呢?
require_once("Abstracts/DBManager.php");
require_once("UI/UI.Package.php");
class BlogDBM extends DBManager
{
private $table = "blog_records";
function saveRecord($title,$url,$desc,$feedId,$pubDate)
{
$PDO = $this->db->connect();
try
{
$query = $PDO->prepare("
INSERT INTO ".$this->table."
(title,url,desc,feed_id,pubdate) VALUES
(:title,:url,:desc,:feed_id,:pubdate)");
$query->bindParam(":title", $title);
$query->bindParam(":url", $url);
$query->bindParam(":desc", $desc);
$query->bindParam(":feed_id", $feedId, PDO::PARAM_INT);
$query->bindParam(":pubdate", $pubDate, PDO::PARAM_INT);
$query->execute();
//return $PDO->lastInsertId();
} catch(PDOException $e)
{
echo "Error " . $e->getMessage();
}
$PDO = NULL;
}
}
This code does not throw an error but the query fails, that is, the execute
method returns false. How could that be?
require_once("Abstracts/DBManager.php");
require_once("UI/UI.Package.php");
class BlogDBM extends DBManager
{
private $table = "blog_records";
function saveRecord($title,$url,$desc,$feedId,$pubDate)
{
$PDO = $this->db->connect();
try
{
$query = $PDO->prepare("
INSERT INTO ".$this->table."
(title,url,desc,feed_id,pubdate) VALUES
(:title,:url,:desc,:feed_id,:pubdate)");
$query->bindParam(":title", $title);
$query->bindParam(":url", $url);
$query->bindParam(":desc", $desc);
$query->bindParam(":feed_id", $feedId, PDO::PARAM_INT);
$query->bindParam(":pubdate", $pubDate, PDO::PARAM_INT);
$query->execute();
//return $PDO->lastInsertId();
} catch(PDOException $e)
{
echo "Error " . $e->getMessage();
}
$PDO = NULL;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您将
PDOStatement::bindValue()
与PDO::PARAM_BOOL
一起使用时,也会发生这种情况。解决方案:只需切换到PDO::PARAM_INT
即可。It also happens when you use
PDOStatement::bindValue()
withPDO::PARAM_BOOL
. Solution: just switch toPDO::PARAM_INT
.当有人关闭/关闭主 id 字段上的数据库自动增量时,可能会出现类似的问题。
Similar problems can occur when someone turns/leaves off the DB autoincrement on the main id field.
这周我为这个无声插入失败而苦苦挣扎。这是对我有用的解决方案。我没有在事务上调用提交,因此插入被置于挂起状态,但从未在数据库上完成 - 因此没有错误。这比较棘手,因为该项目中的 PDR 数据库包装器是一个静态类,因此它不会自动关闭
解决方案:确保在插入/更新/删除操作之后或在页面关闭时在 PDO 句柄上调用提交。
$PDO->exec("COMMIT;");
i struggled with this silent insert fail this week. and here's the solution that worked for me. i was not calling commit on the transaction, so the insert was put into the pending state but was never completed on the database - hence no error. this was trickier because the PDR db wrapper in this project is a static class so it doesn't automatically close
solution: make sure to call commit on the PDO handle after the insert / update / delete actions - or on page close.
$PDO->exec("COMMIT;");
只是想补充一点,由于缺少错误消息而感到类似的挫败感。
要阻止 PDO 静默失败,您可以在 PDO 连接上设置错误模式。
如果您希望出现错误但仍继续,还有
PDO::ERRMODE_WARNING
。Just wanted to add to this, had similar frustrations from the lack of an error message.
To stop PDO from silently failing, you can set the error mode on the PDO connection.
There is also
PDO::ERRMODE_WARNING
if you want errors but still continue.我很确定 MySQL 在
desc
字段名称上被阻塞 - 它是一个 保留字。您必须将其放入“`”引号中,或者更好的是更改字段名称。至于错误报告,请使用 errorInfo 方法。您可以使 PDO 实际上在异常中输出失败查询的结果,但默认行为 - 我认为 - 是仅在根本无法进行查询时抛出异常,但如果查询不会失败有故障。
I'm pretty sure that MySQL chokes on the
desc
field name - it is a reserved word. You'd have to put it into "`" quotes or, better, change the field name.As for error reporting, use the errorInfo method. You can make PDO actually output the result of a failed query in the exception, but the default behaviour - I think - is to throw an exception only if the query can't be made at all, but it doesn't fail if the query is faulty.
我也面临着这个错误。
我使用了
print_r($con->errorInfo());
它在数组的0th
键中给了我0000
。然后我匹配了所有列名称,发现我使用了错误的字段名称。
这拯救了我的日子。
I was also facing that error.
I used
print_r($con->errorInfo());
it gives me0000
in0th
key of array.Then I matched all column names and figured out that I am using wrong field name.
This saves my day.