我試圖計算結果中的行數,並且不斷收到上面返回的錯誤。我檢查了手冊,並且我正在使用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;
}
注:我本來沒有的: – 2011-12-22 17:59:11
注:我本來沒有的「代碼」 $這個 - > store_result( ); '代碼'的功能,只是在一些研究後添加它,並認爲它可能有幫助。 – 2011-12-22 18:00:11
刪除括號。 $這 - > _ result-> NUM_ROWS();到$ this - > _ result-> num_rows;它爲我工作 – 2016-12-14 16:51:27