Zend Database mysq_num_rows() Equivalent

The Zend Database doe not seem to have an exact function for what we PHP/MySQL developers are used to with the mysql_num_rows() function. There are different ways to achieve the same result and I wanted to share my personal favorite with you. Basically you create your SQL statement so that it returns one value, which represents the number of results.

SELECT COUNT(*) AS num FROM TABLE

So to do something like this using the Zend DB class, say you wanted to count all the active users in a table, based on an active flag, would look something like this.

 
$num_rows = $zend_db->fetchOne("SELECT COUNT(`id`) FROM `users` WHERE `active` = 1");

About this entry