3

Is there a way to get the total number of rows in Zend db select like with using SQL_CALC_FOUND_ROWS in a regular mysql query. I haven't been able to find a similar functionality for this apart from running the same query without the limit clause.

3 Answers 3

11
$db->select()
   ->from($tableName, array(
       new Zend_Db_Expr('SQL_CALC_FOUND_ROWS id'), 
       'name', 
       'price'
   ));

You could also try replacing all cols with COUNT(*) and running the query second time. It may actually be more efficient (even if it's counter-intuitive). This was the case for my app.

You can do it like this:

$select->reset('cols')->reset('limit')->cols('COUNT(*)'); //there is a constant for the 'cols' in Select class
$db->query($select);
Sign up to request clarification or add additional context in comments.

Comments

2

Because no one else has anything better to offer I'll suggest to see this post on a ZF forum. It's from 2008 and ZF may have been upgraded since then.

Comments

0

Use this method if data inside the rows also needed

   $rows = $db->select()->from('foo')->query()->fetchAll(); 

    echo 'Total number of rows found : ' . count($rows);

if you just need the count of total number of rows then

$count = $db->select()->from('foo','COUNT(*)')->query()->fetchColumn();

2 Comments

Although it works this is very inefficient especially if you are using MySQL. Using the calc_rows saves retrieval of alot data to find out how many entries there are without any limit for paging. To make it database independend you could also implement a seperate COUNT query.
efficient way added I though that number of total rows were needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.