作者:手机用户2502854251 | 来源:互联网 | 2023-05-17 07:10
IhavebuiltaclasswhichleveragestheabilitiesofPHPsbuilt-inMySQLiclass,anditisintende
I have built a class which leverages the abilities of PHP's built-in MySQLi class, and it is intended to simplify database interaction. However, using an OOP approach, I am having a difficult time with the num_rows instance variable returning the correct number of rows after a query is run. Take a look at a snapshot of my class...
我已经构建了一个利用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 ...
}
Here is a sample usage:
以下是一个示例用法:
$db = new Database();
$result = $db->query("SELECT * FROM `pages`"); //Contains at least one entry
echo $result->num_rows; //Returns "0"
exit;
How come this is not accurate? Other values from result object are accurate, such as "field_count". Any help is greatly appreciated.
怎么这不准确?结果对象中的其他值是准确的,例如“field_count”。任何帮助是极大的赞赏。
Thank you for your time.
感谢您的时间。
2 个解决方案