Count rows with Zend_Db

Posted on December 5, 2006. Filed under: Zend Framework |

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.

Make a Comment

Make a Comment: ( 7 so far )

blockquote and a tags work here.

7 Responses to “Count rows with Zend_Db”

RSS Feed for notes171 Comments RSS Feed

Thanks for the tip. I was looking exactly for this, but couldn’t find it anywhere.

Thanks, it’s very useful for me

Useful for me, too. Thanx!

Nice :)

Just what I needed!

That’s the simple sample.
What if I need to count number of rows with some complex select() with several joins and groups.

Thanks

Thanks Guy, i looked for something like this for hours.

awesome!! really helpful


Where's The Comment Form?

  • Bookmarks

Liked it here?
Why not try sites on the blogroll...