当我将第一条记录添加到数据库表中时,它不会显示在显示记录的页面上。但是当我添加第二条记录并打开时,它们会显示在页面上,但我输入的第一条记录除外。
这是我的代码:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
while ($row = mysql_fetch_array($result))
{
echo "<table>";
echo"<tr><th><B>Player Name</B><Th><B>Role</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>". $myrow['player_name']. "</td>";
echo "</td>";
echo "<td>" .$myrow['player_role']. "</td>";
echo "</tr>";
}
echo "</table>";
}
有人可以告诉我有什么问题吗?
答案 0 :(得分:2)
它可能是由嵌套的while()
循环引起的。无需使用嵌套的while()
。请改用一个while()
。例如:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysql_fetch_array($result))
{
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";
答案 1 :(得分:0)
while()
循环内。mysqli
您的代码(已更改)
$result = mysqli_query($db,"SELECT * FROM members ORDER BY player_role DESC");
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";