Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined function mysqli_result::num_rows()

Tags:

php

mysqli

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.

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):

$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;
}
like image 458
Brian Vanderbusch Avatar asked Dec 22 '11 17:12

Brian Vanderbusch


People also ask

What is result -> Num_rows?

Returns the number of rows in the result set. The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. This function returns 0 for unbuffered result sets unless all rows have been fetched from the server.

What does mysqli_ num_ rows return?

The mysqli_num_rows() function returns the number of rows in a result set.


1 Answers

From the manual, it seems that mysqli_result::num_rows isn't a function, but rather a variable containing the number of 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_result object, but that's if you're using the procedural style rather than object oriented style.

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

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),

public function count()
{
    // Any other processing you want
    // ...
    return mysqli_num_rows($this->_result);
}
like image 159
Rich Adams Avatar answered Oct 23 '22 14:10

Rich Adams