Search code examples
zend-frameworkzend-db

Zend Db query to select all IDs


How would I write an Zend DB query to select all from the column ID?

So far I have tried:

public function getLatestUserID()
    {
        $ids = $this->select()
             ->where('id = ?');
        return $ids;
    }

But to no avail.


Solution

  • You just want the id column,
    You failed to call an execute command.

    try:

    //assuming you are using a DbTable model
    public function getLatestUserID()
        {
            $ids = $this->fetchAll('id');
            return $ids;
        }
    

    I would do it like this, because I use the select() object for everything:

    public function getLatestUserID()
        {
            $select = $this->select();
            //I'm not sure if $this will work in this contex but you can out the table name
            $select->from(array($this), array('id'));
            $ids = $this->fetchAll($select);
            return $ids;
        }
    

    The first two examples should return just the id column of the table, now if you actually want to query for a specific id:

     public function getLatestUserID($id)
            {
                $select = $this->select();
                $select->where('id = ?', $id);
                //fetchAll() would still work here if we wanted multiple rows returned
                //but fetchRow() for one row and fetchRowset() for multiple rows are probably
                //more specific for this purpose.
                $ids = $this->fetchRow($select);
                return $ids;
            }