SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: Counting Total Records
-
Jun 5, 2007, 06:58 #1
- Join Date
- Apr 2006
- Location
- Nairobi, Kenya
- Posts
- 93
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Counting Total Records
Hi,
I have a quesry that sometimes retrieves very many records. This made it take a while. So i Introduced LIMIT and Previous and Next links.
This all works quite well.
My problem is that I'd like to know how many records in total will be returned (in all the pages) but i fear that this would be the same us running the query without the LIMIT, which would make it slow again.
How can I overcome this?
AbeLife is too short to think small - John Mason
What is any life if not the pursuit of a dream? - Vanilla Sky
PHP Membership Script
-
Jun 5, 2007, 07:11 #2
- Join Date
- Apr 2007
- Location
- Pakistan
- Posts
- 102
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can run a query with COUNT() e.g.
PHP Code:$rs = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM table"));
$total_results = $rs[0];
PHP Code:$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 0,10");
$rs = mysql_query(mysql_query("SELECT FOUND_ROWS()"));
$total_results = $rs[0];
$rs = mysql_fetch_array($result);
-
Jun 5, 2007, 07:22 #3
- Join Date
- Apr 2006
- Location
- Nairobi, Kenya
- Posts
- 93
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Life is too short to think small - John Mason
What is any life if not the pursuit of a dream? - Vanilla Sky
PHP Membership Script
-
Jun 5, 2007, 07:24 #4
- Join Date
- Apr 2007
- Location
- Pakistan
- Posts
- 102
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is that faster than actually running the original unlimited query?
-
Jun 5, 2007, 08:28 #5Code MySQL:
select sql_calc_found_rows * from table limit 10; --the normal query select found_rows(); -- gives you the total rows
-
Jun 7, 2007, 07:03 #6
- Join Date
- Apr 2006
- Location
- Nairobi, Kenya
- Posts
- 93
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks.
Life is too short to think small - John Mason
What is any life if not the pursuit of a dream? - Vanilla Sky
PHP Membership Script
Bookmarks