I'm getting the following error in my code. I am following a book for help and I triple checked my code with the books and it's still not working. I am able to display the code when all I have is view_users.php, so the first page displays the 5 users, but when I click on the NEXT in my pagination code I get the error. I'm guessing that the
view_users.php?s=0$np=3
?s=0$np=3 is messing up my code and I'm not sure why.
Any help would be great since my eyes are bugging me from looking at the screen the past 4 1/2 hours.
Book I used is "Larry Ullman - PHP And MySQL for dynamic Web sites - Second edition, page 301 in case anyone has the book.
And the error points to this line of code.:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
::::ERROR BELOW;::::
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
<?php
require_once ('mysql_connect.php'); //Connects to the database.
$display = 5; // How many records to display at a time.
if (isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$query = "SELECT COUNT(*) FROM reg_users ORDER BY reg_registered ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
if ($num_records > $display) {
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
} // End of np IF.
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
$query = "SELECT reg_lastname, reg_firstname, DATE_FORMAT(reg_registered, '%M %d, %Y') AS dr, reg_ID FROM reg_users ORDER BY reg_registered ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Registered</b></td>
</tr>
';
// Fetch and print all the records.
$bg = '#eeeeee'; //Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td align="left">' . $row['reg_lastname'] . '</td>
<td align="left">' . $row['reg_firstname'] . '</td>
<td align="left">' . $row['dr'] . '</td>
</tr>
';
}
echo '</table>';
// mysql_free_result($result); //Free up the resources.
//mysql_close(); // Close the database connection
// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo '<br /><p>';
// Determine what page the scrip is on.
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button
if ($current_page !=1) {
echo '<a href="view_users.php?s=' . ($start - $display) . '$np=' . $num_pages . '">Previous</a> ';
}
// Make all the numbered pages
for ($i = 1; $i <= $num_pages; $i++){
if ($i != $current_page) {
echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '$np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ' ;
}
}
// If it's not the last page, make a Next button.
if ($current_page != $num_pages){
echo '<a href="view_users.php?s=' . ($start + $display) . '$np=' . $num_pages . '">Next</a>';
}
echo '</p>';
} // End of links section.
?>