egalp Posted January 9, 2007 Share Posted January 9, 2007 Is there a way to use one query to get the number of results that fit a certain criterion and then filter them?Suppose that I have many rows that fit a criterion, and I want to limit the actual number of rows I fetch, dividing their display to pages that show only a certain number of results per page. I would use the LIMIT argument, with the proper offset to display them. But I would also like to know the total number of results so I could calculate the number of pages and generate a page navigation display (i.e. page numbers).Right now I am using two seperate queries, which seems a bit wasteful.Something like:[code]//Geting total number of rows$query = "SELECT id FROM p_poll";$result = mysql_query($query);$num = mysql_num_rows($result);//Issueing filtered query$query = "SELECT * FROM p_poll ORDER BY time DESC LIMIT $offset, $per_page";$result = mysql_query($query,$connect);$pages = (int) $num / $per_page + 1;//Outputing query datawhile($row = mysql_fetch_array($result)){ echo "Various crap";}//Generating page navigationfor($i=1;$i<$pages;$i++){ if($i != $page) { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">[".$i."]</a> "; } else { echo "-".$i."- "; }}[/code] Quote Link to comment Share on other sites More sharing options...
btherl Posted January 10, 2007 Share Posted January 10, 2007 In Mysql, using the default MyISAM tables, it is very fast to count the number of rows in a table like this:[code=php:0]$query = "SELECT count(*) FROM p_poll";$result = mysql_query($query) or die(mysql_error());$row = mysql_fetch_array($result);if ($row) { $num = $row[0];}[/code]I am rusty with mysql, so I may have made a mistake in there somewhere. Quote Link to comment Share on other sites More sharing options...
weknowtheworld Posted January 10, 2007 Share Posted January 10, 2007 Else can have :$query = "SELECT * FROM p_poll";$result = mysql_query($query) or die(mysql_error());$ i =0;$row = mysql_fetch_array($result);if ($row) { $i++;}echo "Count : $i"; Quote Link to comment Share on other sites More sharing options...
fenway Posted January 10, 2007 Share Posted January 10, 2007 [quote author=weknowtheworld link=topic=121650.msg501111#msg501111 date=1168409280]Else can have :$query = "SELECT * FROM p_poll";$result = mysql_query($query) or die(mysql_error());$ i =0;$row = mysql_fetch_array($result);if ($row) { $i++;}echo "Count : $i";[/quote]Please, please PLEASE NEVER do this. Use a count() if you want to count, run a query if you want to examine each matching record in succession. Quote Link to comment Share on other sites More sharing options...
egalp Posted January 11, 2007 Author Share Posted January 11, 2007 Thanks, but I wasn't asking how to count rows... (If you look at the code piece I submitted I'm counting them at the beginning)I was asking if it's possible to combine a count on all rows and fetch a filtered result on that count in the same query. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 11, 2007 Share Posted January 11, 2007 The comment was directed at weknowtheworld, not you, egalp.... perhaps what you're looking for is SQL_CALC_FOUND_ROWS? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.