[英]Php MySql While loop if result?
Is there a way to tell if there is results before the while loop using this style? 有没有办法告诉使用这种风格的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()) {
}
I would like to do something before and after the while loop but I do not want to query twice. 我想在while循环之前和之后做一些事情,但我不想再查询两次。
To check the number of rows selected: $stmt->num_rows
检查所选行数:
$stmt->num_rows
So you might do: 所以你可能会这样做:
if($stmt->num_rows > 0){
...
}
http://www.php.net/manual/en/mysqli-stmt.num-rows.php http://www.php.net/manual/en/mysqli-stmt.num-rows.php
Try 尝试
stmt = $mysqli->prepare("SELECT * FROM items WHERE type = ?");
$stmt->bind_param('s', $type);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($one,$two,$three);
$rows = $stmt->num_rows;
if ($rows) {
// your code
}
while ($stmt->fetch()) {
}
you can check with mysqli -> num_rows before while loop 你可以在while循环之前检查mysqli - > num_rows
$stmt = $mysqli->prepare("SELECT * FROM items WHERE type = ?");
$stmt->bind_param('s', $type);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($one,$two,$three);
$numrows = $stmt->num_rows;
if ($numrows > 0) {
while ($stmt->fetch()) {
}
} else {
echo "No rows found";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.