PHPの組み込みMySQLiクラスの能力を活用するクラスを構築しました。これはデータベースのやりとりを簡単にするためのものです。しかし、OOPアプローチを使用すると、クエリの実行後にnum_rowsインスタンス変数が正しい数の行を返すのが困難です。私のクラスのスナップショットを見てみましょう...ここでPHP MySQLi num_rows常に0を返します
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"など正確です。どんな助けでも大歓迎です。
ありがとうございます。
Gahhの
等価デフォルトフラグを使用する必要があります...それはとても簡単でした。それを指摘してくれてありがとう。 PHPサイトのサンプルと私のサンプルクエリの両方が "MYSQLI_USE_RESULT"定数を使用していました。私はそれを削除し、それは魅力のように動作します!助けてくれてありがとう、フィル! –
MYSQLI_USE_RESULTの使用に関する注記: [** PHP mysql :: query **](http://es1.php.net/manual/en/mysqli.query.php)_ MYSQLI_USE_RESULTを使用すると、それ以降のすべての呼び出しはreturn error [** mysqli_free_result()**](http://es1.php.net/manual/en/mysqli-result.free.php)を呼び出さない限り、 –