我有以下代码,用于检查数据库(对于类)的约束。您正在尝试获取查询返回的行数,但我不断收到相同的错误
$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 aSELECT
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 aSELECT
statement. Instead, usePDO::query()
to issue aSELECT COUNT(*)
statement with the same predicates as your intended SELECT statement, then usePDOStatement::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/