html mysql_fetch_array跳过第一行

pkwftd7m  于 2022-11-27  发布在  Mysql
关注(0)|答案(6)|浏览(129)

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

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

8wigbo561#

您正在调用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>";
bogh5gae

bogh5gae2#

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

epggiuax3#

程式码中有额外的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>";
4bbkushb

4bbkushb4#

初始化$row时调用mysql_fetch_array,读取第一行,然后在开始while循环时第二次调用它,在循环运行之前将它移动到第二行,因此它不会对第一行执行任何操作。

gpnt7bae

gpnt7bae5#

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

xbp102n06#

要解决此问题,只需注解掉该行$row = mysql_fetch_array($results);。查询将提取该行之后的每一行。

相关问题