Problem with mysql_num_rows
| From: | Todd Williamsen | Date: | Wed, 22 May 2002 01:27:02 +0000 |
| Subject: | Problem with mysql_num_rows | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-19341@lists.php.net to get a copy of this message | ||
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>
\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>
\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";
}
?>