简体   繁体   English

PHP mySQL numRows() 用法和错误?

[英]PHP mySQL numRows() usage and errors?

I have the following code that I am using to check for constraints on a database(for a class).我有以下代码用于检查数据库(对于一个类)的约束。 U am trying to get the number of rows returned by the query and I keep getting the same error on the line您正在尝试获取查询返回的行数,但我一直在线上遇到相同的错误

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

Error:错误:

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

I've been pulling my hair out because my other functions similar to this one work fine, this is the only function that doesn't work.我一直在拔头发,因为与此类似的其他功能工作正常,这是唯一不起作用的 function。 Is there something that stands out in this one?有什么突出的吗?

The argument $db is just the connection to my database, pno is an integer and the essn is text.. So I'm not sure what I am doing wrong..参数 $db 只是到我的数据库的连接, pno是 integer 而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 is not an Object , but a boolean , as stated in PHP PDOStatement::rowcount documentation. $ires1不是Object ,而是boolean ,如PHP PDOStatement::rowcount文档中所述。

A warning though, from the PHP.net site:但是,来自 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.如果关联PDOStatement执行的最后一个 SQL 语句是SELECT语句,则某些数据库可能会返回该语句返回的行数。 However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.但是,不能保证所有数据库都具有此行为,并且不应依赖于可移植应用程序。

There you have their suggested solution too:您也有他们建议的解决方案:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.对于大多数数据库, PDOStatement::rowCount()不会返回受SELECT语句影响的行数。 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.相反,使用PDO::query()发出SELECT COUNT(*)语句,其谓词与您的预期 SELECT 语句相同的谓词,然后使用PDOStatement::fetchColumn() Your application can then perform the correct action."然后,您的应用程序可以执行正确的操作。”

I did not know and I couldn't find information on method numRows , so that's as far as I can go.我不知道,也找不到有关方法numRows的信息,所以就我可以 go 而言。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM