【发布时间】:2019-09-13 09:48:47
【问题描述】:
我不知道如何在我的 while 循环中使用 foreach。
我已经尝试理解这些帖子,但我无法弄清楚:
我也尝试在其他网站(如 w3)上找到它。
这是我的代码:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM data";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table'>
<tr>
<th>ID</th>
<th>date</th>
<th>security</th>
<th>photo</th>
</tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["id"]. "</td>
<td>" . $row["Date"]."</td>
<td>" . $row["Security"]. "</td>
<td>" . $row["Photo"]. "</td>
</tr></table>";
}
} else {
echo "No data found";
}
$conn->close();
?>
我从数据库表中只得到一行,其他的输出如下:
2019-09-12 15:24:23 0 6 2019-09-12 15:30:09 1 7 2019-09-12 15:30:33 1 8 2019-09-12 15:30:33 1 9 2019-09-12 15:30:33 1 10 2019-09-12 15:32:39 1 11 2019-09-12 15:32:39 1 12 2019-09-12 15:32:39 1 13 2019 -09-12 15:32:39 1 14 2019-09-12 15:32:39 1 15 2019-09-12 15:32:39 1 16 2019-09-12 19:26:36 1 17 2019-09 -12 19:26:42 1 18 2019-09-12 19:35:15 1 19 2019-09-12 19:35:15 1 20 2019-09-12 19:35:15 1 21 2019-09-12 19:35:15 1 22 2019-09-12 19:35:15 1 23 2019-09-12 19:35:15 1 24 2019-09-12 19:35:15 1 25 2019-09-12 19: 35:15 1 26 2019-09-12 19:35:15 1 27
【问题讨论】: