2011-12-22 30 views
5

我試圖計算結果中的行數,並且不斷收到上面返回的錯誤。我檢查了手冊,並且我正在使用mysqli_result :: num_rows()(我使用的是面向對象的風格)。我有三個類在這裏工作。調用未定義函數mysqli_result :: num_rows()

類(連接):

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 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(); 
    } 
} 

類功能:

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; 
    } 
} 

我使用調用所有這一切的測試頁是(包括聲明只是__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; 
} 
+0

注:我本來沒有的: – 2011-12-22 17:59:11

+0

注:我本來沒有的「代碼」 $這個 - > store_result( ); '代碼'的功能,只是在一些研究後添加它,並認爲它可能有幫助。 – 2011-12-22 18:00:11

+0

刪除括號。 $這 - > _ result-> NUM_ROWS();到$ this - > _ result-> num_rows;它爲我工作 – 2016-12-14 16:51:27

回答

6

從手冊中,似乎mysqli_result::num_rows不是一個函數,而是一個包含行數的變量。

它可以像這樣使用:

$num_rows = $mysqli_result->num_rows; 

的功能相當於是mysqli_num_rows($result),在那裏你在mysqli_result對象傳遞,但這是,如果你正在使用的程序的風格,而不是面向對象的風格。

在你的代碼,你應該改變你count()功能在utils_MysqlImprovedResult類是這樣的(我假定這就是你得到錯誤信息功能),

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

或者相反,如果要混用OO和程序樣式(可能是一個壞主意),

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

是不是它在這裏如何使用?我檢查了手冊,這也正是它在手冊中的用法。 – 2011-12-22 18:08:09

+0

在你的代碼中你有「$ this - > _ result-> num_rows();」,這不是它在手冊中的用法。在手冊中它被描述爲「int $ mysqli_result-> num_rows;」 (注意缺少函數大括號「()」)。將其更改爲「return $ this - > _ result-> num_rows;」在你的代碼中,它應該工作。 – 2011-12-22 18:12:38

+0

謝謝......這是我在這裏的第一個問題。快樂與雅做生意。 – 2011-12-22 18:19:27

相關問題