10

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

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

5 回答 5

16

您正在调用 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>";
于 2013-08-15T17:58:46.037 回答
6
$query = "SELECT * FROM members";
$results = mysql_query($query);
$row = mysql_fetch_array($results); <-- There's your first row. Remove this.
于 2013-08-15T17:56:58.777 回答
2

代码中有额外内容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>";
于 2013-08-15T18:10:22.987 回答
0

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

于 2013-08-15T17:57:27.077 回答
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>";
于 2017-05-08T17:42:23.133 回答