問題描述
我正在嘗試計算結果中的行數,但不斷收到上述返回的錯誤.我已經檢查了手冊,我正在使用 mysqli_result::num_rows() ,因為我應該使用(我使用的是面向對象的風格.)我在這里工作了三個類.
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 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() 函數):
The test page I'm using to call all this is (include statement is just __autoload() function that is working fine):
$columnvar = 'emailaddress@test.com';
$pass = 'blah';
require_once 'inc.init.php';
$user = new utils_User();
try{
$string = $user->resetPassword($email, $pass);
echo $string;
}
catch(Exception $e) {
echo $e;
}
推薦答案
從手冊上看,mysqli_result::num_rows 不是一個函數,而是一個包含行數的變量.
From the manual, it seems that mysqli_result::num_rows isn't a function, but rather a variable containing the number of rows.
可以這樣使用:
$num_rows = $mysqli_result->num_rows;
等價的函數是mysqli_num_rows($result)
,你在其中傳入mysqli_result
對象,但前提是你使用的是過程風格而不是面向對象風格.
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.
在您的代碼中,您應該將 utils_MysqlImprovedResult
類中的 count()
函數更改為如下所示(我假設這是您獲得的函數)錯誤信息),
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;
}
或者如果你想混合 OO 和程序風格(可能是個壞主意),
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);
}
這篇關于調用未定義的函數 mysqli_result::num_rows()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!