Search code examples
phpzend-frameworkcountzend-db-select

Chech if any rows were selected with Zend_Db_Select


I'm selecting a post from my DB table and I'm getting the id from the url with getParam(). What I want to do is, show an error message when there's no posts with the id specified in the url.

This is the query I have:

$db = Zend_Registry::get('db');
$select = $db->select();
$select->from(array('p' => 'posts'))
       ->join(array('u' => 'users'), 'u.user_id = p.post_userid')
       ->where('p.post_id = ?', $postid);
$post = $db->fetchRow($select);

Problem is, when i do echo count($post) it shows 1 even when the id is invalid, and it shows more than 1 when the id is valid and a row was actually selected.

So my question is, how should I check how many rows were selected with the id specified? ($postid!).

Any suggestions?


Solution

  • If there are no results fetchRow will return false, so you can test directly on the result, like: if($post) { ... And this is the reason that count returns 1, calling count on anything other than an array returns 1 (except on a null variable). Note that this is different to the fetchRow method of Db_Table, that returs null.