Tôi đang cố gắng đếm số hàng trong một kết quả và tôi tiếp tục nhận được lỗi trả về ở trên. Tôi đã kiểm tra các hướng dẫn sử dụng, và tôi đang sử dụng mysqli_result :: num_rows() như tôi nên (Tôi đang sử dụng phong cách hướng đối tượng.) Tôi đã có ba lớp học làm việc ở đây.Gọi để không xác định chức năng 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 (Xử lý kết quả): function
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:
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;
}
}
Trang thử nghiệm Tôi đang sử dụng để gọi tất cả điều này là (lệnh include chỉ là hàm __autoload() đang hoạt động tốt):
$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;
}
Lưu ý: tôi ban đầu không có: –
Lưu ý: tôi ban đầu không có 'mã' $ this-> store_result (); 'mã' trong funciton, chỉ cần thêm nó sau khi một số nghiên cứu và nghĩ rằng nó có thể giúp đỡ. –
Xóa các dấu ngoặc vuông. $ this -> _ result-> num_rows(); đến $ this -> _ result-> num_rows; Nó làm việc cho tôi –