2011-10-11 94 views
4

我正在運行一個相當直接的mysql請求並將結果返回給一個表。數據庫中有三條記錄,查詢從兩個表中提取。因此,我得到了三條記錄(回顯mysql_num_rows)的計數,但只有兩條顯示在表中。在數組結果上使用print_r命令只顯示一個特定的記錄 - 其他記錄顯示在print-r中..我向db添加了另一條記錄,現在有三條記錄顯示 - 並且與以前相同的記錄不顯示和是print_r命令中的唯一記錄。 下面是相關代碼:PHP mysql_fetch_array沒有返回所有行 - 一行總是被忽略

<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行出現在表中。那裏有任何想法?

謝謝 - 唐

+0

作爲一個澄清:你堅持'print_r($ row)'在你的'while循環中嗎? –

+0

你可能會考慮學習'PDO',這將被棄用。 – 2011-10-11 03:41:26

回答

8

每次通話時間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());,因爲它無論如何不會起任何作用。

+0

感謝大家回答 - 一旦指出答案是顯而易見的。爲了簡單起見,我檢查了許多正確答案中的第一個。再次感謝。 – user988732

5

您正在獲取while循環外的第一個結果。

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); 
0

由於其他親密,問題是,你打電話給mysql_fetch_array()一次,在$result = mysql_query(...之後的行上,在你進入你的while循環之前。這需要從結果中獲得第一行,但是您絕不會對它做任何事情。然後,當你開始你的while循環時,你再次調用mysql_fetch_array(),但由於你已經佔據第一行,所以從第二行開始。

0

好吧你必須明白爲什麼忽略1行讓我們看看$ row = mysql_fetch_array($ result)或die(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>