Zend DB fetchAll(): where clause array with IN operator
Asked Answered
T

3

15

I'm selecting records from a database using the equivalent of this query:

SELECT * FROM reports WHERE user_id IN (3, 6, 22);

The function calling fetchAll() has an argument that's an array of the user IDs, and this call works just fine:

$resultSet = $this->getDbTable()->fetchAll('user_id IN (' . implode(', ', $userIds) . ')');

However, I would like to use an array for the where clause because there will probably be other restrictions to the query later... and I can't figure it out for the life of me. I thought it would be some variation on the following:

$resultSet = $this->getDbTable()->fetchAll(array('user_id IN ?' => '(' . implode(', ', $userIds) . ')'));

But so far no dice. Can someone provide the correct syntax here?

Thermotherapy answered 17/2, 2010 at 20:55 Comment(0)
B
23
$data = array(1, 2, 3);
$select->where('user_id IN (?)', $data);
Bagworm answered 17/2, 2010 at 21:2 Comment(1)
Well, it's for fetchAll() instead of where(), but that's the ticket. I had tried 'user_id IN (?)' before but associated it with the result from implode(), not just the array directly.Thermotherapy
I
2

In Zend 2

$data = array(1, 2, 3);
$select->where('user_id', $data);
Insufflate answered 23/1, 2014 at 18:6 Comment(0)
N
0
$select = $this->getSql()->select();
$select->where("reports.user_id in ('3','6','22')"); 
$resultSet = $this->selectWith($select);             
//echo $select->getSqlString();die;
return $resultSet;
Nearly answered 8/8, 2016 at 10:4 Comment(1)
Although this code may help to solve the problem, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add explanation, including what limitations and assumptions apply.Misreport

© 2022 - 2024 — McMap. All rights reserved.