MySQLi Prepared Statements默认是unbuffer的结果集,这意味着我们不能使用num_rows
,除非我们显式地使用$stmt->store_result();
创建一个缓冲的结果集。
在我的情况下,我只需要使用$stmt->num_rows
来确保有一个结果来自数据库。所以我用了
if($stmt->num_rows > 0){
while($stmt->fetch())
{
echo '<li>'.$name.'</li>';
}
}else {
echo "No Such a Data";
}
现在我的问题是,如果我不想将未缓冲的查询更改为缓冲的查询,那么检查数据库服务器中是否有结果的替代方法是什么?
您可以简单地检查返回的语句是否为资源,然后查看行数,然后根据需要获取。
$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
}
}