2

I have a mysqli query whose where clause is generated in a for loop. So parameters are not known before runtime.

How can i use mysqli bind_param method in this case?

Can i use it in the for loop to bind parameters one by one?

mike_x_
  • 1,900
  • 3
  • 35
  • 69

1 Answers1

8

Yes it's possible and very simple with php5.6, first you need know how many paraments str_repeat(), count() can you help and the unpacking operator (...) too, so this way is possible make binds dynimic.

$params = [10, 50, 51, 99];
$types = str_repeat('i',count($params));

$stmt = $mysqli->prepare("SELECT * FROM t WHERE id IN (?,?,?,?)");
$stmt->bind_param($types, ...$params);
if(!$stmt->excute()){
    echo mysqli_error($con);
}
rray
  • 2,518
  • 1
  • 28
  • 38