PHP mysql_fetch_array没有返回所有行 - 总是忽略一行

时间:2011-10-11 03:18:27

标签: php

我正在运行一个相当直接的mysql请求并将结果返回到表中。数据库中有三条记录,查询是从两个表中提取的。结果,我得到了三个记录的数量(回显mysql_num_rows),但只有两个显示在表中。在数组结果上使用print_r命令只显示一条特定的记录 - 其他记录确实显示在print-r ..我在数据库中添加了另一条记录,现在有三条记录显示 - 并且与之前相同的记录未显示是print_r命令中的唯一记录。 这是相关的代码:

<td id="page1"> 
  <?php             
    $limit  = 15;             // Set limit to show for pagination
    $page   = $_GET['page'];  // get page number from submit

    if($page) 
      $start = ($page - 1) * $limit; // first item to display on this page
    else
      $start = 0; // if no page var is given, set start to 0

    $query  = "SELECT PartyMstr.PartyMstrID, UserName, FirstName, LastName, XrefPartyRoleID
                FROM PartyMstrRole, PartyMstr
                WHERE PartyMstr.PartyMstrID = PartyMstrRole.PartyMstrID &&
                      PartyMstrRole.XrefPartyRoleID = 1 
                ORDER BY LastName, FirstName ASC
                LIMIT $start, $limit
              ";

    $result = mysql_query($query, $connection);

    $row = mysql_fetch_array($result) or die(mysql_error());
    $totalitems1 =  mysql_num_rows($result);
  ?>
  <center><h3> Admin User List </h3></center>
  <?php
    echo "<table border=\"1\" align=\"center\">";
    echo "<tr><th>PartyMaster ID</th>";
    echo "<th>UserName</th>";
    echo "<th>Last, First</th>";
    echo "<th>Link</th></tr>";

    while($row = mysql_fetch_array($result)) {
      echo "<tr><td>";
      echo $row['PartyMstrID'];
      echo "<td>";
      echo $row['UserName'];
      echo "<td>";
      echo " " . $row['LastName'] . ", " . $row['FirstName'] . " ";
      echo "<td>";
      echo "<a href = \"http://www.505575.com/editUser.php?id=" . $row['PartyMstrID'] . "\" >Edit</a>";

      // echo "<td>";
      // echo $row['XrefPartyRoleID'];

      echo "</td></tr>";
    }

    echo "</table><br/><br/> ";

    $paginaton  = getPaginationString( $page, $totalitems, $limit,
                    $adjacents  = 1,
                    $targetpage = "adminUserList.php",
                    $pagestring = "?page="
    ); // Functon found in functions.php

    echo $paginaton; 
  ?>
</td>

我花了很多时间在网上寻找解释却没有成功。我已关闭$pagination代码行而没有效果。我尝试了各种其他技巧并回应输出。返回的行数(n)始终正确,但表中仅显示n-1行。那里有什么想法吗?

谢谢 - 唐

5 个答案:

答案 0 :(得分:9)

每次拨打mysql_fetch_array时,您都会从资源中搜索一行。当资源没有更多行要提供时,它返回false。这就是while ($a = mysql_fetch_array($resource))循环工作的方式。

    $result = mysql_query($query, $connection);
    $row = mysql_fetch_array($result) or die(mysql_error());    
    $totalitems1 =  mysql_num_rows($result);

    // first row is taken from resource

    ....

    while($row = mysql_fetch_array($result))

    // now take the rest of the rows

正如您所看到的,您的代码完全按照您的要求进行操作!只需删除第一个$row = mysql_fetch_array($result) or die(mysql_error());,因为它无论如何都不会用于任何目的。

答案 1 :(得分:5)

您正在while循环之外获取第一个结果。

答案 2 :(得分:1)

    $query = "SELECT PartyMstr.PartyMstrID, UserName, FirstName, LastName, XrefPartyRoleID
        FROM PartyMstrRole, PartyMstr 
        WHERE PartyMstr.PartyMstrID = PartyMstrRole.PartyMstrID  && PartyMstrRole.XrefPartyRoleID = 1 
        ORDER BY LastName, FirstName ASC
        LIMIT $start,$limit";
    $result = mysql_query($query, $connection);
    $row = mysql_fetch_array($result) or die(mysql_error());    
    $totalitems1 =  mysql_num_rows($result);

需要:

    $query = "SELECT PartyMstr.PartyMstrID, UserName, FirstName, LastName, XrefPartyRoleID
        FROM PartyMstrRole, PartyMstr 
        WHERE PartyMstr.PartyMstrID = PartyMstrRole.PartyMstrID  && PartyMstrRole.XrefPartyRoleID = 1 
        ORDER BY LastName, FirstName ASC
        LIMIT $start,$limit";
    $result = mysql_query($query, $connection);
    $totalitems1 =  mysql_num_rows($result);

答案 3 :(得分:0)

正如其他人的亲密关系一样,问题是您在进入mysql_fetch_array()循环之前,在$result = mysql_query( ...之后的行上呼叫while一次。这将从您的结果中获取第一行,但您从未对其执行任何操作。然后当你开始while循环时再次调用mysql_fetch_array(),但由于你已经占据了第一行,它从第二行开始。

答案 4 :(得分:0)

好吧你必须明白为什么它忽略1行让我们看到 $ row = mysql_fetch_array($ result)或者死(mysql_error()); 这段代码已经获取你的第一行然后你获取循环所以它在已经提取了行之后指向了行。

 $limit  = 15;             // Set limit to show for pagination
    $page   = $_GET['page'];  // get page number from submit

    if($page) 
      $start = ($page - 1) * $limit; // first item to display on this page
    else
      $start = 0; // if no page var is given, set start to 0

    $query  = "SELECT PartyMstr.PartyMstrID, UserName, FirstName, LastName, XrefPartyRoleID
                FROM PartyMstrRole, PartyMstr
                WHERE PartyMstr.PartyMstrID = PartyMstrRole.PartyMstrID &&
                      PartyMstrRole.XrefPartyRoleID = 1 
                ORDER BY LastName, FirstName ASC
                LIMIT $start, $limit
              ";

    $result = mysql_query($query, $connection);

    $row = mysql_fetch_array($result) or die(mysql_error());
    $totalitems1 =  mysql_num_rows($result);
  ?>
  <center><h3> Admin User List </h3></center>
  <?php
    echo "<table border=\"1\" align=\"center\">";
    echo "<tr><th>PartyMaster ID</th>";
    echo "<th>UserName</th>";
    echo "<th>Last, First</th>";
    echo "<th>Link</th></tr>";

    while($row = mysql_fetch_array($result)) {
      echo "<tr><td>";
      echo $row['PartyMstrID'];
      echo "<td>";
      echo $row['UserName'];
      echo "<td>";
      echo " " . $row['LastName'] . ", " . $row['FirstName'] . " ";
      echo "<td>";
      echo "<a href = \"http://www.505575.com/editUser.php?id=" . $row['PartyMstrID'] . "\" >Edit</a>";

      // echo "<td>";
      // echo $row['XrefPartyRoleID'];

      echo "</td></tr>";
    }

    echo "</table><br/><br/> ";

    $paginaton  = getPaginationString( $page, $totalitems, $limit,
                    $adjacents  = 1,
                    $targetpage = "adminUserList.php",
                    $pagestring = "?page="
    ); // Functon found in functions.php

    echo $paginaton; 
  ?>
</td>