Question

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();
Was it helpful?

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

OTHER TIPS

There's no parameter there so it doesn't need to be an array. Assuming you know $lifetime is a safe, integer value, try:

 ->where('data > SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND)');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top