I get an invalid argument when trying to create this output of pages to
limit to 20 records per pages.  I removed the line and it stopped the error,
but the next and previous buttons will go on and on even if there is no more
records so I am thinking it is my mysql_num_rows() function

I have gotten this script from here:

http://www.phpbuilder.com/columns/rod20000221.php3

If you know of a better one or can help me out on this one, that is much
appreciated

Actual error is:

not a valid mySQL result resource

here is the code:

<?php

   $numresults = mysql_query("SELECT * FROM booklist");
  $numrows = mysql_num_rows($numresults);
             < ---------------------------------------- FAILS HERE
// next determine if offset has been passed to script, if not use 0
print "Pages:";
if (empty($offset)) {
    $offset=1;
 }
$connection = @mysql_connect("localhost", "", "") or die ("could not connect
to database");
$db = @mysql_select_db("db", $connection) or die("Could not select
database");
$sql = "SELECT id,title, author, subject FROM booklist LIMIT $offset,
$limit";
$limit = 20;

// get results
$result = mysql_query($sql, $connection) or die("Could not execute query");
while ($row = mysql_fetch_array ($result))

echo
"<tr><td>".$row['title']."</td><td>".$row['author']."</td><td>".$row['subjec
t']."</td></tr>";
}
// next we need to do the links to other results

if ($offset==1) { // bypass PREV link if offset is 0
    $prevoffset=$offset-20;
    print "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> &nbsp; \n";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from
division
if ($numrows%$limit) {
    // has remainder so add one page
    $pages++;
}

for ($i=1;$i<=$pages;$i++) { // loop thru
    $newoffset=$limit*($i-1);
    print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> &nbsp; \n";
}

// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
    // not last page so give NEXT link
    $newoffset=$offset+$limit;
    print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}

?>



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to