我构建了一个利用 PHP 内置 MySQLi 类功能的类,它旨在简化数据库交互。但是,使用 OOP 方法,我很难使用 num_rows 实例变量在运行查询后返回正确的行数。看看我的课堂截图...
class Database {
//Connect to the database, all goes well ...
//Run a basic query on the database
public function query($query) {
//Run a query on the database an make sure is executed successfully
try {
//$this->connection->query uses MySQLi's built-in query method, not this one
if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
return $result;
} else {
$error = debug_backtrace();
throw new Exception(/* A long error message is thrown here */);
}
} catch (Exception $e) {
$this->connection->close();
die($e->getMessage());
}
}
//More methods, nothing of interest ...
}
这是一个示例用法:
$db = new Database();
$result = $db->query("SELECT * FROM `pages`"); //Contains at least one entry
echo $result->num_rows; //Returns "0"
exit;
这怎么不准确?结果对象的其他值是准确的,例如“field_count”。任何帮助是极大的赞赏。
感谢您的时间。