PDO IN() Array Statement AND a placeholder

前端 未结 5 887
死守一世寂寞
死守一世寂寞 2021-01-04 08:16

I found this code on SO, which is great for using PDO and the IN() statement together.

$values = explode(\',\', $values) ; # 1,4,7

$placeholders = rtrim(str         


        
5条回答
  •  [愿得一人]
    2021-01-04 08:55

    Solution

    This should work, if $values is an array:

    $query = "SELECT * FROM table WHERE id IN ($placeholders) AND product=?";
    $stm->execute(array_merge($values, array($product)));
    

    Explanation

    execute() expects one parameter - in this case an array - to be provided. By adding array_merge($values, array($product)) you create one array with $product added at the end, so the query should work correctly.

    See the demo here: http://ideone.com/RcClX

提交回复
热议问题