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」など、結果オブジェクトのその他の値は正確です。どんな助けでも大歓迎です。
お時間をいただきありがとうございます。