我正在从数据库中提取数据,但只将第一行放入动态生成的 html 表中。
我试过添加另一个 foreach 循环,但这不是答案……我没有主意了……
$conn = new PDO('mysql:host=localhost;dbname=jeuxvideo', $dbUserName, $dbPassword);
$sql = "SELECT * FROM jeuxvideo";
$result = $conn->prepare($sql);
$request = $result->execute();
echo "<table border='1'>";
echo "<tr><td>Id</td><td>Titre</td><td>Prix<td>Date de Sortie</td><td>Genre</td><td>Origine</td><td>Mode</td><td>Connexion</td></tr>\n";
$row = $result->fetch(PDO::FETCH_ASSOC);
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";
此代码提取所有正确信息并将其放在 html 表中的正确位置,但由于某些原因它没有收集以下行数据...
最佳答案
这一行只获取一行:
$row = $result->fetch(PDO::FETCH_ASSOC);
您需要获取尽可能多的行。一旦没有更多行可获取,该函数将返回 FALSE
,因此您可以使用 while
循环,如下所示:
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
关于php - 从数据库中提取数据并放入 html 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482464/