mysql - PHP MySQLi num_rows Always Returns 0 -
i have built class leverages abilities of php's built-in mysqli class, , intended simplify database interaction. however, using oop approach, having difficult time num_rows instance variable returning correct number of rows after query run. take @ snapshot of class...
class database { //connect database, goes ... //run basic query on database public function query($query) { //run query on database make sure executed try { //$this->connection->query uses mysqli's built-in query method, not 1 if ($result = $this->connection->query($query, mysqli_use_result)) { return $result; } else { $error = debug_backtrace(); throw new exception(/* long error message thrown here */); } } catch (exception $e) { $this->connection->close(); die($e->getmessage()); } } //more methods, nothing of interest ... }
here sample usage:
$db = new database(); $result = $db->query("select * `pages`"); //contains @ least 1 entry echo $result->num_rows; //returns "0" exit;
how come not accurate? other values result object accurate, such "field_count". appreciated.
thank time.
possible bug: http://www.php.net/manual/en/mysqli-result.num-rows.php#104630
code source above (johan abildskov):
$sql = "valid select statement yields results"; if($result = mysqli-connection->query($sql, mysqli_use_result)) { echo $result->num_rows; //zero while($row = $result->fetch_row()) { echo $result->num_rows; //incrementing 1 each time } echo $result->num_rows; // total count }
could validate procedural style:
/* determine number of rows result set */ $row_cnt = mysqli_num_rows($result);
Comments
Post a Comment