PHP mySQL numRows() 用法和错误?

标签 php mysql mysql-num-rows

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

$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/

相关文章:

php - 用PHP搜索查询

php - mysql_num_rows 的问题

mysql - Mysql Workbench 中模型未与数据库同步

mysql - 散列两个相等的字符串会给出相同的散列值

php - JQuery 验证远程和检查数据库 PHP MySQL 错误

php - mysql_num_rows() expects parameter 1 to be resource 错误

php - 在 wordpress 中进行查询?

php - 如何限制登录尝试 - PHP & MySQL & CodeIgniter

php - 确定生成的日期是否属于假期(PHP)?

php - 使用 mysql 和 php 的复选框更新多行