gpt4 book ai didi

php - 无法检索 PDOStatement 受影响的行数

转载 作者:行者123 更新时间:2023-11-29 05:23:50 26 4
gpt4 key购买 nike

使用 PDO 连接到 MySQL。我无法为“INSERT INTO”查询检索受影响的行数

$sql = 'insert into ... ';
$q = $dbh->prepare($sql);
$q = $q->execute();
echo ( $q->rowCount() );
echo ( $q->affectedRows() );

Fatal error: Call to a member function rowCount() on a non-object.
Fatal error: Call to a member function affectedRows() on a non-object.

记录已成功插入,但我无法检查它,因为它不会返回任何内容。怎么了?

最佳答案

当您调用PDOStatement::execute() 时,您不应该覆盖$q 变量。 ;它返回一个 bool 值。在调用 execute 方法时简单地删除赋值:

$q->execute();

PDOStatement 中也没有名为affectedRows 的方法。 PDOStatement::rowCount()应该是你所需要的:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

关于php - 无法检索 PDOStatement 受影响的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22544667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com