php 调用未定义的函数 mysqli_result::num_rows()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8607984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:04:31  来源:igfitidea点击:

Call to undefined function mysqli_result::num_rows()

phpmysqli

提问by Brian Vanderbusch

I'm trying to count the number of rows in a result, and I keep getting the above returned error. I've checked the manual, and I'm using mysqli_result::num_rows() as I should be (I'm using object oriented style.) I've got three classes working here.

我正在尝试计算结果中的行数,但不断收到上述返回的错误。我已经检查了手册,并且我正在使用 mysqli_result::num_rows() ,因为我应该使用(我使用的是面向对象的风格。)我在这里工作了三个类。

Class (Connection):

类(连接):

class utils_MysqlImprovedConnection {
    protected $_connection;

    public function __construct($host, $user, $pwd, $db)
    {
        $this->_connection = @new mysqli($host, $user, $pwd, $db);
        if(mysqli_connect_errno ()) {
            throw new RuntimeException('Cannot access database:' . mysqli_connect_error());
        }
    }

    public function getResultSet($sql)
    {
        $results = new utils_MysqlImprovedResult($sql, $this->_connection);
        return $results;
    }

    public function  __destruct() {
        $this->_connection;
    }
}

Class (Handles Result):

类(处理结果):

class utils_MysqlImprovedResult implements Iterator, Countable {
    protected $_key;
    protected $_current;
    protected $_valid;
    protected $_result;


    public function  __construct($sql, $connection) {
       if (!$this->_result = $connection->query($sql)){
           throw new RuntimeException($connection->error . '. The actual query submitted was: '. $sql);
       }
    }

    public function  rewind()
    {
        if (!is_null($this->_key)){
            $this->_result->data_seek(0);
        }
        $this->_current = $this->_result->fetch_assoc();
        $this->_valid = is_null($this->_current) ? false : true;
    }
    public function valid()
    {
        return $this->_valid;
    }
    public function current()
    {
        return $this->_current;
    }
    public function key()
    {
        return $this->_key;
    }
    public function next()
    {
        $this->_current = $this->_result->fetch_assoc();
        $this->_valid = is_null($this->_current) ? false : true;
        $this->_key++;
    }
    public function count()
    {
        $this->_result->store_result();
        $this->_result->num_rows();
    }
}

Class function:

类功能:

public function resetPassword($email, $pass){
    //check if email exists, update authkey and password, send email
    $sql = "SELECT * FROM table WHERE column = '$email'";
    $results = $this->_db->getResultSet($sql);
    if($results->count() == 1){
        // Process
        $this->_message = "Success!";
        return $this->_message;
    } else {
        // Not unique
        $this->_error = "Try again";
       return $this->_error;
    } 
}

The test page I'm using to call all this is (include statement is just __autoload() function that is working fine):

我用来调用所有这些的测试页面是(include 语句只是 __autoload() 函数,它工作正常):

$columnvar = '[email protected]';
$pass = 'blah';
require_once 'inc.init.php';
$user = new utils_User();
try{
   $string = $user->resetPassword($email, $pass);
   echo $string;
}
catch(Exception $e) {
   echo $e;
}

回答by Rich Adams

From the manual, it seems that mysqli_result::num_rowsisn't a function, but rather a variable containing the number of rows.

从手册来看,mysqli_result::num_rows似乎不是一个函数,而是一个包含行数的变量。

It can be used like this:

它可以像这样使用:

$num_rows = $mysqli_result->num_rows;

The function equivalent is mysqli_num_rows($result), where you pass in the mysqli_resultobject, but that's if you're using the procedural style rather than object oriented style.

等效的函数是mysqli_num_rows($result),您在其中传递mysqli_result对象,但前提是您使用的是过程样式而不是面向对象的样式。

In your code, you should change your count()function in the utils_MysqlImprovedResultclass to be like this (I'm assuming that's the function where you're getting the error message),

在您的代码中,您应该count()utils_MysqlImprovedResult类中的函数更改为这样(我假设这是您收到错误消息的函数),

public function count()
{
    // Any other processing you want
    // ...
    return $this->_result->num_rows;
}

or alternatively if you want to mix OO and procedural styles (probably a bad idea),

或者,如果你想混合 OO 和程序风格(可能是个坏主意),

public function count()
{
    // Any other processing you want
    // ...
    return mysqli_num_rows($this->_result);
}