2017-06-16 34 views
0

Can I bind an array to an IN() condition?PDO:配列を複数のWHERE句でIN()条件にバインドしますか?

上記の質問には、複数のWHERE句を使用した場合の回答はありません。私は、追加WHERE句を入れた場合、それは空の結果やエラーが発生し

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

次善の答えは1つのWHERE句でのみ動作します。

$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)); 

助言してください。

答えて

1

executeは、パラメータの単一の配列を受け入れます。あなたはする必要があります:

$sth->execute(array_merge($ids, [$location_id])); 
関連する問題