Count rows with Zend_Db
There are several ways to count rows in a table using the Zend_Db component. The first way is to fetch the rows in a row set object and run count() on it. As you see it isn’t really smart to build a row set object if you just want to know the number of rows. There is a better way:
$query = $db->select()->from('my_table', 'count(*)');
$numRows = $db->fetchOne($query);
You can perfectly embed this in your table model and use “where” conditions and joins to get the right numbers.
Regards to Justin Plock for this one.
Thanks for the tip. I was looking exactly for this, but couldn’t find it anywhere.
Luis
January 26, 2007
Thanks, it’s very useful for me
Thanh Duc
July 6, 2007
Useful for me, too. Thanx!
Symphony
October 22, 2007
Nice
Just what I needed!
Kyle
March 12, 2008
That’s the simple sample.
What if I need to count number of rows with some complex select() with several joins and groups.
Thanks
Den
April 22, 2008
Thanks Guy, i looked for something like this for hours.
Aza
February 8, 2009
awesome!! really helpful
Kfir Ozer
October 13, 2009