Search code examples
zend-frameworkzend-dbzend-studiozend-db-table

Zend db select how to know return zero rows?


This is my code:

public function is_existing($url) {
    $query = $this->select()
        ->where("url=?",$url);

    if(!$query) {
        return false;
    } else {
        $row = $this->fetchRow($query);
        $row = $row->toArray();
        return $row;
    }
}

But sometimes, I receive

Fatal error: Call to a member function toArray() on a non-object in...

Is it right to use if(!$query) to check the existing rows in DB?


Solution

  • Change your code to this

    public function is_existing($url) {
        $query = $this->select()
            ->where("url=?",$url);
    $row = $this->fetchRow($query);
        if(!$row) {
            return false;
        } else {
    
            return $row->toArray();
    
        }
    }
    

    Because $query object will always be created irrespective $url matches record or not hence will always pass if condition .