zend-dbzend-db-select

Error in zend_db_select statement to return as new column


I am trying the return with a new column named count. Here is my query. $userid is the paramter to pass in

        $select = $this->db->select()
            ->from('myfriend')
            ->where('fromid=?', $userid)
            ->join('user', 'user.userid = friends.toid')
            ->columns( new Zend_Db_Expr('Select count(*) where friends.toid = $userid as count'))
            ->order("id desc")
            ->limit(20);

I want to return a new column called count. where it will display number of rows where friends.toid = userid.

Seems to have some syntax error.


Solution

  • You must use double quotes, not single quotes, so php can eval $userid, like "Select count(*) where friends.toid = $userid as count"

    If you want to debug your query, you can always use:

    echo $select->__toString()