Performing SELECT COUNT using Zend_Db_Select

Written by James Mansson on January 3, 2013 Categories: Databases, Zend Framework 1

Following on from my previous post, another common variant on the SELECT statement is SELECT COUNT. For instance:

SELECT COUNT(*) FROM order WHERE status = 'ordered'

This statement returns the number of records in the order table where the status field is set to ordered.

To perform this query using Zend_Db_Select, we would use something like:

$select = $dbTable->select();
$select->from($dbTable, array('count(*) as c'));
$rows = $dbTable->fetchAll($select);
$count = $rows[0]->c;

Note that we alias the result of the account as c in order to make retrieval easier.

No Comments on Performing SELECT COUNT using Zend_Db_Select

Leave a Reply

Your email address will not be published. Required fields are marked *