gpt4 book ai didi

php - 使用php从数据库获取数据到html表

转载 作者:行者123 更新时间:2023-11-29 20:06:16 24 4
gpt4 key购买 nike

我想从数据库中获取数据并将其插入到我的html表中,这是我的代码,我不知道我的错误在哪里:

    <div class="ibox-content">
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, lastname, phone FROM regis";
$result = $conn->query($sql);
$conn->close();
?>
<table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Acc Type</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>";
<td>0795934799</td>
<td class="center">demo@demo.com</td>
<td>Admin</td>
}
echo "</table>";
}
?>
</tbody>
</table>
</div>

我从数据库获取的数据显示在表之外。抱歉英语不好:)

最佳答案

您的 while 循环中只有一个语法错误。您需要打印 echo 命令中的所有内容。正如您在您的文件中所看到的,您有 echo(...); ,后面跟着一些仍在 php.ini 中的 HTML。因此,您应该将其更改为以下内容来更正它。

while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>
<td>0795934799</td>
<td class=\"center\">demo@demo.com</td>
<td>Admin</td>";
}

您可以使用的一个很好的资源是 phpchecker.com检查您的代码是否有错误

关于php - 使用php从数据库获取数据到html表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40338521/

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