Search code examples
mysqlzend-frameworkzend-framework2zend-dbzend-db-table

Zend Db Sql Where


Hi how can I do a query like this in zf2 with zend\db\sql?

Query:

SELECT * FROM table WHERE field = $field AND data > SUBDATE(NOW(), INTERVAL 1 DAY)

In ZF2

$select = $this->sql->select();
$select->from(self::MYTABLE)
           ->where(array('fiels' => $field))
           ->where(array('data > ' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute()->current();

Solution

  • change the line

    ->where(array('data > ' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
    

    to

    ->where(array('data > ?' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
    

    From the code snippet, it's seen you had missed the place holder for the parameter(?), include a question mark, I had mentioned the existing line of code and the modified code for quick reference