Re: Fast count of recordset in php...

From: Date: Sun, 07 Aug 2005 13:04:53 +0000
Subject: Re: Fast count of recordset in php...
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-220170@lists.php.net to get a copy of this message
On 7 Aug 2005, at 05:04, TalkativeDoggy wrote:
be coz this way is slow and costs more overload. $sql = "SELECT COUNT(IDVara) cn FROM tbvara WHERE Varunamn LIKE '$checkLev%'"; $querys = mysql_query($sql); //Count products in db // $dbArray = mysql_fetch_row($querys); $nrOfProducts = $dbArray[0];
According to the docs, MySQL has a particular optimisation that means that it should be: $sql = "SELECT COUNT(*) FROM tbvara WHERE Varunamn LIKE '$checkLev%'"; Also if you want the full count value when you've done a query that uses a LIMIT clause, instead of doing a separate full count query, you can get it by using the SQL_CALC_FOUND_ROWS keyword, e.g. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM tbvara WHERE Varunamn LIKE '$checkLev%' LIMIT 10"; Of course count(*) could only ever return up to 10 in this query - you can find out how many it would have found by then asking: $sql = "SELECT FOUND_ROWS()"; Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture marcus@synchromedia.co.uk | http://www.synchromedia.co.uk

« previous php.general (#220170) next »