mysql_fetch_array跳过第一行

时间:2013-08-15 17:54:54

标签: php html mysql

编辑:感谢快速回复的人 - 我最终切换到mysql_fetch_assoc()并使用了do ... while,我很高兴。

我正在使用相同的过程来创建和填充表格 - 除了在每个表格中跳过第一行之外,一切正常。可以使用另一双眼睛,因为我似乎无法确定问题。提前谢谢。

   $query = "SELECT * FROM members";
   $results = mysql_query($query);
   $row = mysql_fetch_array($results);


   //echo my <table> start and headings;

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

5 个答案:

答案 0 :(得分:15)

你正在调用mysql_fetch_array两次......一次在循环之前,然后一次循环。

如果您需要一个种子行来构建标题行,那么在这里使用do .. while循环可能会更好。

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

答案 1 :(得分:6)

$query = "SELECT * FROM members";
$results = mysql_query($query);
$row = mysql_fetch_array($results); <-- There's your first row. Remove this.

答案 2 :(得分:1)

代码中有额外的mysql_fetch_array。此外,缺少打开表格标记(<table>)。

更正后的代码:

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

答案 3 :(得分:0)

初始化$ row时调用mysql_fetch_array,后者读取第一行。然后在开始while循环时再次调用它,在运行任何循环之前将其移动到第二行。因此,它从不对第一行做任何事情。

答案 4 :(得分:0)

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