gpt4 book ai didi

PHP 将 MySQL 数据回显到 HTML 表中

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

所以我正在尝试制作一个 HTML 表,从 MySQL 数据库获取数据并将其输出给用户。我正在用 PHP 这样做,我对它非常陌生,所以请原谅我的 messy code !

我使用的代码是:应对“你的代码太糟糕了!”的 Storm

<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = mysql_connect("localhost", "notarealuser", 'notmypassword');
for ($i = 1; $i <= 20; $i++) {
$items = ($mysqli->query("SELECT id FROM `items` WHERE id = $i"));
echo ("<tr>");
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['id'];
}</td>");
$items = ($mysqli->query("SELECT name FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['name'];
}</td>");
$items = ($mysqli->query("SELECT descrip FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['descrip'];
}</td>");
$items = ($mysqli->query("SELECT reward FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['reward'];
}</td>");
$items = ($mysqli->query("SELECT img FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['img'];
}</td>");
echo ("</tr>");
}
?>
</tbody>
</table>

但是,此代码不起作用 - 它只是导致页面立即输出 500 Internal Server Error。 IIS 日志将其显示为 500:0 - 通用 ISE。有什么想法吗?

最佳答案

您混合了 mysql 和 mysqli,没有关闭 php 代码块,也没有选择数据库。另外,您不必为每个字段运行查询

试试这个:

<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>

<tbody>
<?php
$con = new mysqli("host","user", "password", "database");

$execItems = $con->query("SELECT id, name, descrip, reward, img FROM `items` WHERE id BETWEEN 1 AND 20 ");

while($infoItems = $execItems->fetch_array()){
echo "
<tr>
<td>".$infoItems['id']."</td>
<td>".$infoItems['name']."</td>
<td>".$infoItems['descrip']."</td>
<td>".$infoItems['reward']."</td>
<td>".$infoItems['img']."</td>
</tr>
";

}
?>
</tbody>
</table>

关于PHP 将 MySQL 数据回显到 HTML 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721451/

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