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
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.
You are fetching the first result outside your while loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With