gpt4 book ai didi

PHP mySQL numRows() 用法和错误?

转载 作者:行者123 更新时间:2023-11-29 09:10:07 25 4
gpt4 key购买 nike

我有以下代码,用于检查数据库(对于类)的约束。您正在尝试获取查询返回的行数,但我不断收到相同的错误

$count1= $ires1->numRows(MDB2_FETCHMODE_ASSOC);

错误:

> Call to a member function numRows() on a non-object

我一直在抓狂,因为我的其他与此类似的功能工作正常,这是唯一不起作用的功能。这其中有什么突出的地方吗?

参数 $db 只是到我的数据库的连接,pno 是一个整数,essn 是文本..所以我不确定我在做什么错误..

<?php
function submitCheck($db){
$essn= $_POST['essn'];
$pno=$_POST['pno'];

$query1 = "select * from works_on where pno=? and essn=?";
$types1 = array('integer','text');
$stmt1 = $db->prepare($query1, $types1, MDB2_PREPARE_MANIP);

if (MDB2::isError($stmt1)) {
print("bad prepared statement:" . $stmt->getMessage());
}

$queryargs1 = array($pno, $essn);
$ires1 = $stmt1->execute($queryargs1);
$count1= $ires1->numRows(MDB2_FETCHMODE_ASSOC);
//print("The project number entered was $count1[pno]");
if(!(count($count1)==0)){
print("The employee is already part of this project! If you want to update the hours, please select update!");
return false;
}
return true;
}
?>

最佳答案

$count1 = $stmt1->rowCount();

$ires1 不是一个 Object,而是一个 boolean,如 PHP PDOStatement::rowcount 中所述。文档。

来自 PHP.net 站点的警告:

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

您也有他们建议的解决方案:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT
COUNT(*)
statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action."

我不知道,也找不到有关方法 numRows 的信息,所以这就是我所能做的。祝你好运!

关于PHP mySQL numRows() 用法和错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5824310/

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