MySQLi Prepared Statements are Unbuffered result set by default which means we can not use the num_rows
unless we explicitly create a buffered result set by using $stmt->store_result();
.
In my case I just need to use the $stmt->num_rows
to make sure that there is a result comming from database. so I used something like
if($stmt->num_rows > 0){
while($stmt->fetch())
{
echo '<li>'.$name.'</li>';
}
}else {
echo "No Such a Data";
}
now my question is if I would not like to change the unbuffured query to buffered one what is the alternative method to check there is a result in the database server?
You can simply check if the returned statement is a resource and then look at the row count, then fetch as appropriate.
$stmt = $mysqliObject->query("SELECT * FROM table");
if(is_resource($stmt) && $stmt->num_rows > 0) {
// we got a result and have data in this table
while($item = $stmt->fetch()) {
// $item is our data from this row
}
}