Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysql_fetch_array is not returning all rows - one row is always ignored

Tags:

php

I am running a fairly straight forward mysql request and returning the results to a table. There were three record in the db, and the query is pulling from two tables. As a result, I am getting a count of three records (echoing mysql_num_rows), but only two show in the table. Using a print_r command on the array result shows only one particular record - the other records do show in the print-r.. I added another record to the db, and now three records show - and the same record as before does not show and is the only record in the print_r command. Here's the relevant code:

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

I've spent a lot of time online looking for an explanation without success. I've switched off the $pagination code line without effect. I have tried various other tricks and echoed output. The number of rows returned (n) is always correct, but only n-1 rows appear in the table. Any ideas out there?

Thanks - Don

like image 769
Don Clark Avatar asked Dec 06 '22 18:12

Don Clark


2 Answers

Every time you call mysql_fetch_array you are taking a row from the resource. When the resource has no more rows to give, it returns false. That's how while ($a = mysql_fetch_array($resource)) loops work.

    $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

As you can see, your code is doing exactly what you tell it to! Just remove the first $row = mysql_fetch_array($result) or die(mysql_error()); as it doesn't serve any purpose anyway.

like image 178
Mahdi.Montgomery Avatar answered Dec 10 '22 12:12

Mahdi.Montgomery


You are fetching the first result outside your while loop.

like image 20
K-ballo Avatar answered Dec 10 '22 13:12

K-ballo