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?
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);
}