How to use mysqli bind_param dynamically

前端 未结 1 1436
别那么骄傲
别那么骄傲 2020-12-01 22:58

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?

相关标签:
1条回答
  • 2020-12-01 23:34

    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);
    }
    
    0 讨论(0)
提交回复
热议问题