gpt4 book ai didi

php - 如何使用 SQL 表行 ID 作为 div 的 ID 来回显 div?

转载 作者:行者123 更新时间:2023-11-29 07:05:00 25 4
gpt4 key购买 nike

我有这个 PHP 代码,它从名为“comments”的 SQL 数据库中获取数据。此代码打印出表中的每条评论:

<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);

if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
}
echo '<div class = "white_line_comments"></div>';
} else {
echo "0 results";
}

到目前为止,一切都运行良好,一切都按预期打印。然后我决定想要一种方法来给每个单独的评论某种识别,使它们独一无二。我尝试将每个注释放入其自己的 div 中,使用 SQLtable 行 id 作为 div 的 id。

但是,当我现在尝试访问我的网页时,它告诉我该网站无法运行(HTTP 错误 500)。

<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);

if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
echo '
<div class="white_line_comments"></div>';
} else {
echo "0 results";
}

对此有什么想法吗?我想我在包含 div 时一定做错了什么,但我不知道是什么!

最佳答案

启动 while 循环后,此行出现错误:

echo "<div class ='$row['id'].'>";

应该是

echo "<div class ='". $row['id'] ."'>";

您还应该配置您的 Web 服务器/托管/本地主机以引发 PHP 错误。

如果您在本地主机或您自己的服务器上,请阅读此内容: How can I make PHP display the error instead of giving me 500 Internal Server Error

如果您使用共享主机,请阅读此内容:How do I displaying details of a PHP internal server error?

关于php - 如何使用 SQL 表行 ID 作为 div 的 ID 来回显 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com