php 这个 PDO 准备好的语句返回 false 但不抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2518354/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:47:29  来源:igfitidea点击:

This PDO prepared statement returns false but does not throw an error

phpmysqlinsertpdoprepared-statement

提问by Simsevu

This code does not throw an error but the query fails, that is, the executemethod returns false. How could that be?

这段代码没有抛出错误但查询失败,即该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;
     }
}

采纳答案by Pekka

I'm pretty sure that MySQL chokes on the descfield name - it is a reserved word. You'd have to put it into "`" quotes or, better, change the field name.

我很确定 MySQL 会在desc字段名称上窒息- 它是一个保留字。您必须将其放入 "`" 引号中,或者更好的是更改字段名称。

As for error reporting, use the errorInfomethod. 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.

至于报错,使用errorInfo方法。您可以让 PDO 在异常中实际输出失败查询的结果,但默认行为 - 我认为 - 是仅在根本无法进行查询时才抛出异常,但如果查询不会失败是错误的。

回答by WhoIsRich

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.

要阻止 PDO 静默失败,您可以在 PDO 连接上设置错误模式。

$dbh = new PDO();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

There is also PDO::ERRMODE_WARNINGif you want errors but still continue.

还有PDO::ERRMODE_WARNING如果你想要错误但仍然继续。

回答by ????? ???

I was also facing that error.

我也面临那个错误。

I used print_r($con->errorInfo());it gives me 0000in 0thkey of array.

我用print_r($con->errorInfo());它给我00000th阵列的关键。

Then I matched all column names and figured out that I am using wrong field name.

然后我匹配了所有列名并发现我使用了错误的字段名。

Thissaves my day.

节省了我的一天。

回答by Jakub Wojnowski

It also happens when you use PDOStatement::bindValue()with PDO::PARAM_BOOL. Solution: just switch to PDO::PARAM_INT.

当您使用PDOStatement::bindValue()with时也会发生这种情况PDO::PARAM_BOOL。解决方案:只需切换到PDO::PARAM_INT.

回答by xeo

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

本周我在这个无声插入失败中挣扎。这是对我有用的解决方案。我没有在事务上调用提交,因此插入被置于挂起状态但从未在数据库上完成 - 因此没有错误。这比较棘手,因为这个项目中的 PDR db 包装器是一个静态类,所以它不会自动关闭

solution: make sure to call commit on the PDO handle after the insert / update / delete actions - or on page close.

解决方案:确保在插入/更新/删除操作之后或在页面关闭时调用 PDO 句柄上的 commit。

$PDO->exec("COMMIT;");

$PDO->exec("COMMIT;");

回答by DragonLord

Similar problems can occur when someone turns/leaves off the DB autoincrement on the main id field.

当有人在主 id 字段上关闭/离开数据库自动增量时,可能会发生类似的问题。