php - 使用相同的 SELECT 重复 WHILE
问题描述
为什么我不能使用相同的while
两次?
我想在页面上重复相同的内容,但我不能,只需加载第一个。
例子:
$rs = $mysqli->query("SELECT * FROM site WHERE id = '".$cliente."' ");
$row = $rs->fetch_assoc();
While 1(位于网站顶部)
<?php do{ ?>
<option value="<?php echo $ID; ?>">
<?php echo $nome; ?>
</option>
<?php } while ($row = $rs->fetch_assoc()); ?>
While 2(位于站点末尾)
<?php do{ ?>
<option value="<?php echo $ID; ?>">
<?php echo $nome; ?>
</option>
<?php } while ($row = $rs->fetch_assoc()); ?>
谢谢你。
解决方案
Store your results in an array instead, and loop that where needed. Otherwise you will need to reset the cursor of your result before looping again.
$query = $mysqli->query("SELECT * FROM site WHERE id = '".$cliente."' ");
$result = $query->fetch_all(MYSQLI_ASSOC);
// Loop wherever needed
foreach ($result as $r) {
// Use $r
}
// Loop the result again!
foreach ($result as $r) {
// Use $r
}
Also recommend you use a prepared statement instead of using a direct query with variables injected into it.
推荐阅读
- python - CSV 阅读器错误地返回行的第一个元素
- php - 启动 Symfony 项目时出现此错误
- automation - 从 MS Forms 提交触发的 PowerBI 仪表板将作为 PDF 通过电子邮件发送
- java - MainActivity 从我的列表中检索项目的 ID,我想将它发送到一个类以创建操作,但是
- javascript - 单击的按钮应保持悬停状态,其他按钮应禁用
- cypress - 在赛普拉斯中,我如何检查动作的响应时间
- excel - 下标超出范围/列表对象存在但未找到
- ansible - Ansible 中是否有等效的 grep 和 cut 命令而不使用命令模块?
- excel - VBA用于根据同一行中的另一个单元格值过滤一行中的单元格
- kotlin - 如何在 TornadoFX 中打开多个编辑器视图