此 PDO 准备好的语句返回 false 但不会引发错误

发布于 2024-08-26 05:23:32 字数 930 浏览 5 评论 0原文

这段代码不会抛出错误,但查询失败,即 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空袭的梦i 2024-09-02 05:23:33

当您将 PDOStatement::bindValue()PDO::PARAM_BOOL 一起使用时,也会发生这种情况。解决方案:只需切换到PDO::PARAM_INT即可。

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

不甘平庸 2024-09-02 05:23:33

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

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

巴黎盛开的樱花 2024-09-02 05:23:33

这周我为这个无声插入失败而苦苦挣扎。这是对我有用的解决方案。我没有在事务上调用提交,因此插入被置于挂起状态,但从未在数据库上完成 - 因此没有错误。这比较棘手,因为该项目中的 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;");

花开雨落又逢春i 2024-09-02 05:23:32

只是想补充一点,由于缺少错误消息而感到类似的挫败感。

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

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

如果您希望出现错误但仍继续,还有 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.

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

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

野鹿林 2024-09-02 05:23:32

我很确定 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.

只等公子 2024-09-02 05:23:32

我也面临着这个错误。

我使用了 print_r($con->errorInfo()); 它在数组的 0th 键中给了我 0000

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

拯救了我的日子。

I was also facing that error.

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

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

This saves my day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文