2017-06-16 70 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); 

如果我把一個附加條款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]));