Php MySql While 循环如果有结果?

标签 php mysql

有没有办法判断使用这种样式的while循环之前是否有结果?

$stmt = $mysqli->prepare("SELECT * FROM items WHERE type = ?");
$stmt->bind_param('s', $type);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($one,$two,$three); 
while ($stmt->fetch()) {

}

我想在 while 循环之前和之后做一些事情,但我不想查询两次。

最佳答案

检查选中的行数:$stmt->num_rows

所以你可能会这样做:

if($stmt->num_rows > 0){
    ...
}

http://www.php.net/manual/en/mysqli-stmt.num-rows.php

关于Php MySql While 循环如果有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23982652/

相关文章:

php - 尝试回应准备好的声明

php - 如何在php中使id升序

mysql - TINYINT(M) 中列长度的用途是什么?

php - 如何通过其键获取 WooCommerce 购物车项目数据数组?

php - 基于 Yii(关系)数据库表动态创建 html 表?

php - 使用mysql从多个表中选择

php - 从多个表中选择顺序和组

mysql - 如何在 bash 中识别\n

javascript - PHP 中的 JS charCodeAt 等价物(具有完整的 unicode 和 emoji 兼容性)

php - 如何从等于多个值的字段中提取 mysql 结果