mysql_fetch_array skipping first row

后端 未结 5 995
旧巷少年郎
旧巷少年郎 2020-11-30 15:25

EDIT : Thanks for the quick responses guys - I ended up switching to mysql_fetch_assoc() and using a do...while and I am good to go.

I am using this

相关标签:
5条回答
  • 2020-11-30 15:29
    while ($row) 
    {
    echo "<tr><td>".$row['Last Name']."</td>";
    echo "<td>".$row['First Name']."</td>";
    echo "<td>".$row['Middle Name']."</td>";
    echo "<td>".$row['Sfx']."</td>";
    echo "<td>".$row['Prf']."</td>";
    echo "<td>".$row['Spouse/SO']."</td>";
    echo "<td>".$row['Ancestor']."</td>";
    echo "<td>".$row['Status']."</td>";
    echo "<td>".$row['Address 1']."</td>";
    echo "<td>".$row['Address 2']."</td>";
    echo "<td>".$row['City']."</td>";
    echo "<td>".$row['ST']."</td>";
    echo "<td>".$row['Zip 5']."</td>";
    echo "<td>".$row['Zip 4']."</td>";
    echo "<td>".$row['Home Phone']."</td>";
    echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>';
      $row = mysql_fetch_array($results);
    
    }
    
    echo "</table><hr>";
    
    0 讨论(0)
  • 2020-11-30 15:30
    $query = "SELECT * FROM members";
    $results = mysql_query($query);
    $row = mysql_fetch_array($results); <-- There's your first row. Remove this.
    
    0 讨论(0)
  • 2020-11-30 15:42

    You call mysql_fetch_array when you initialize $row, which reads the first row. You then call it a second time when you begin your while loop, which moves it to the second row before any of your loop is ran. Hence, it never does anything with the first row.

    0 讨论(0)
  • 2020-11-30 15:46

    There is extra mysql_fetch_array in the code. Also, opening table tag(<table>) is missing.

    Corrected code:

    $query = "SELECT * FROM members";
    $results = mysql_query($query);
    echo "<table>";
    
    while ($row = mysql_fetch_array($results)) 
    {
    echo "<tr><td>".$row['Last Name']."</td>";
    echo "<td>".$row['First Name']."</td>";
    echo "<td>".$row['Middle Name']."</td>";
    echo "<td>".$row['Sfx']."</td>";
    echo "<td>".$row['Prf']."</td>";
    echo "<td>".$row['Spouse/SO']."</td>";
    echo "<td>".$row['Ancestor']."</td>";
    echo "<td>".$row['Status']."</td>";
    echo "<td>".$row['Address 1']."</td>";
    echo "<td>".$row['Address 2']."</td>";
    echo "<td>".$row['City']."</td>";
    echo "<td>".$row['ST']."</td>";
    echo "<td>".$row['Zip 5']."</td>";
    echo "<td>".$row['Zip 4']."</td>";
    echo "<td>".$row['Home Phone']."</td>";
    echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>';
    }
    
    echo "</table><hr>";
    
    0 讨论(0)
  • 2020-11-30 15:52

    You are calling mysql_fetch_array twice... once before the loop then once while looping.

    If you need a seed row for use in building your header row you might be better served with a do.. while loop here.

    $query = "SELECT * FROM members";
    $results = mysql_query($query);
    $row = mysql_fetch_assoc($results);
    
    //echo my <table> start and headings;
    
    do  
    {
        echo "<tr><td>".$row['Last Name']."</td>";
        echo "<td>".$row['First Name']."</td>";
        echo "<td>".$row['Middle Name']."</td>";
        echo "<td>".$row['Sfx']."</td>";
        echo "<td>".$row['Prf']."</td>";
        echo "<td>".$row['Spouse/SO']."</td>";
        echo "<td>".$row['Ancestor']."</td>";
        echo "<td>".$row['Status']."</td>";
        echo "<td>".$row['Address 1']."</td>";
        echo "<td>".$row['Address 2']."</td>";
        echo "<td>".$row['City']."</td>";
        echo "<td>".$row['ST']."</td>";
        echo "<td>".$row['Zip 5']."</td>";
        echo "<td>".$row['Zip 4']."</td>";
        echo "<td>".$row['Home Phone']."</td>";
        echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>';
    } while ($row = mysql_fetch_assoc($results));
    
    echo "</table><hr>";
    
    0 讨论(0)
提交回复
热议问题