fork download
  1. <?php
  2. $my_array=array(1,2,3,4);
  3. // put the array in a session variable
  4. $_SESSION['cart']=$my_array;
  5. // create IN placeholders
  6. $in = str_repeat('?,', count($_SESSION['cart']) - 1) . '?';
  7. $sql1 = "SELECT * FROM product WHERE id IN ($in)";
  8. $params = $_SESSION['cart'] ;
  9. //$stmt = $dbh->prepare($sql1); commented out for demo
  10. //$stmt->execute($params); commented out for demo
  11. echo $sql1, PHP_EOL, print_r($params, true);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
SELECT * FROM product WHERE id IN (?,?,?,?)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)