提问人:James Grunshaw 提问时间:9/29/2021 最后编辑:DharmanJames Grunshaw 更新时间:9/29/2021 访问量:1065
如何遍历mysqli prepared 语句并输出结果?[复制]
How to loop through a mysqli prepared statement and output the results? [duplicate]
问:
我有下面的页面。该数据库包含一个帐户表和一个热点表。热点表中有多个记录,与帐户表的唯一用户 ID 相关联 - 因此,如果用户有 1 个以上的热点,则可能会有 1 个以上的结果。我想做的是遍历结果并从数据库中输出名称(或任何其他详细信息)。我已经尝试了一些东西,但无法让它工作。
这是我的代码:
<?php
include 'header.php';
// Get Hotspots for user from DB
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
// In this case we can use the account ID to get the hotspot info.
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($hsid, $ownerid, $hsname, $hsaddress);
$stmt->fetch();
$stmt->close();
?> <div class="content">
<p <p class="block">Welcome back, <?=$_SESSION['name']?>!</p>
<table>
<tr>
<td>Hotspot Name</td>
</tr>
<tr>
<td><?=$hsname?></td>
</tr>
</table>
</div>
</body>
</html>
答:
0赞
George Sun
9/29/2021
#1
下面将使用 逐个循环遍历所有结果。fetch_assoc();
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$results = $stmt->get_result();
while ($result = $results->fetch_assoc()) {
// do stuff with your result, e.g. `echo '<td>' . $result['id'] . '</td>';
}
$stmt->close();
下面,它在代码中应用,以在单独的行中显示每个结果。hs_name
<?php
include 'header.php';
?>
<div class="content">
<p class="block">Welcome back, <?= $_SESSION['name'] ?>!</p>
<table>
<tr>
<td>Hotspot Name</td>
</tr>
<?php
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$results = $stmt->get_result();
while ($result = $results->fetch_assoc()) {
echo '<tr><td>' . $result['hs_name'] . '</td</tr>'
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
0赞
Dharman
9/29/2021
#2
从 mysqli 预准备语句中迭代结果的最简单方法是获取结果对象 using and use 循环。get_result()
foreach
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$result = $stmt->get_result();
foreach($result as $row) {
echo $row['hs_name'];
}
如果想要更大的灵活性,可以使用循环使用其中一个函数手动迭代。while
fetch_*
大多数情况下,更好的选择是将结果提取到数组中,然后将其传递给显示 HTML 的代码。不要混用数据库逻辑和显示逻辑。
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
<html>
<body>
<div class="content">
<p class="block">Welcome back, <?= $_SESSION['name'] ?>!</p>
<table>
<tr>
<td>Hotspot Name</td>
</tr>
<?php foreach ($rows as $row) : ?>
<tr>
<td><?= $row['hsname'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
如果你使用的是libmysqlclient(你可能不应该),你不能使用,所以你必须单独绑定变量并逐个获取它们。get_result()
$stmt = $con->prepare('SELECT id, owner_id, hs_name, hs_address FROM hotspots WHERE id = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($hsid, $ownerid, $hsname, $hsaddress);
while($stmt->fetch()) {
echo $hsname;
}
上一个:非常慢的循环 PHP
下一个:创建嵌套 SQL 循环
评论