Re: PHP + MySQL, how to get total rows matched when using LIMIT 0, 10?

From: Date: Tue, 05 Nov 2002 22:30:02 +0000
Subject: Re: PHP + MySQL, how to get total rows matched when using LIMIT 0, 10?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-122879@lists.php.net to get a copy of this message
> I'm trying to get the total matched rows when I'm using "LIMIT 0, 10", > but I only get the number 10, when the total should be around 100. So, > can I get total matched rows without doing a separate query using > count()? > > Here is an example of my current query: > > $result = mysql_query("SELECT * FROM products WHERE price<'10' ORDER BY > price LIMIT 0, 10"); > $num_rows = mysql_num_rows($result); Use two queries unless you are using MySQL 4.0+ FOUND_ROWS() Returns the number of rows that the last SELECT SQL_CALC_FOUND_ROWS ... command would have returned, if wasn't restricted with LIMIT. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10; mysql> SELECT FOUND_ROWS(); The second SELECT will return a number indicating how many rows the first SELECT would have returned had it been written without the LIMIT clause. Note that if you are using SELECT SQL_CALC_FOUND_ROWS ... MySQL has to calculate all rows in the result set. However, this is faster than if you would not use LIMIT, as the result set need not be sent to the client. SQL_CALC_FOUND_ROWS is available starting at MySQL version 4.0.0. ---John Holmes...

« previous php.general (#122879) next »