Dark King Dark King - 1 year ago 64
MySQL Question

Table not displaying the first record from database

When I add my first record into the database table, it doesn't show on the page where the records are displayed. But when I add the second record and on, they are displayed on the page except the first record that I entered.

Here is my code:

$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>";

Can someone please tell me what is wrong?

Answer Source
  1. Player Name and Role should not be inside while() loop.
  2. MOST IMPORTANT Do not use mysql, it is deprecated. Instead use mysqli

Your code (changed)

$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>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download