0

Based on Can I bind an array to an IN() condition?

The above question doesn't have any answers for using multiple WHERE clauses.

The second best answer works only with one WHERE clause:

$qMarks = str_repeat('?,', count($ids) - 1) . '?';
$sth = $db->prepare("SELECT * FROM myTable WHERE id IN ($qMarks)");
$sth->execute($ids);

If I put an additional WHERE clause, it produces an empty result or error.

$qMarks = str_repeat('?,', count($ids) - 1) . '?';
$sth = $db->prepare("SELECT * FROM myTable WHERE id IN ($qMarks) AND locationid=?");
$sth->execute(array($ids, ?)); // Also tried $sth->execute($ids, $location_id) and $sth->execute($ids, array($location_id));

Please advise.

Alex G
  • 3,048
  • 10
  • 39
  • 78

1 Answers1

1

execute accepts a single array of parameters. You need to do:

$sth->execute(array_merge($ids, [$location_id]));
Alex Blex
  • 34,704
  • 7
  • 48
  • 75