我使用一个允许我将用户上传到数据库的教程制作了这段代码。整个过程很好,但唯一的问题是它开始一遍又一遍地显示同一个用户中的2个,列表开始扩展5我添加的每个用户...导致这个问题可能是什么原因?
列出整个列表的索引中的项目:
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysql_error());
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
echo $names;
}
} else {
echo "No Users Found";
}
?>
</table>
答案 0 :(得分:1)
output
每次迭代一条记录;或者构建整个HTML块,然后输出块。我认为最简单的是:
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
echo "<tr><td>$user</td></tr>";
}
......替代方法
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysqli_error($db));
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
}
} else {
$names = "No Users Found";
}
echo $names;
?>
</table>
此外,您无法将mysql_*
功能与mysqli_*
一起使用。请参阅http://php.net/manual/en/mysqli.error.php。
问题的最简单示例:https://eval.in/627250