PDO binds n times same value with foreach

后端 未结 3 1183
無奈伤痛
無奈伤痛 2021-01-25 06:15

I have function in PHP, which should bind in MySQL IN statement so many variables, that is in array. My problem is that variable and key is changing but function bind only last

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 06:59

    If you pass the variable as reference it would work fine for value, but won't work for key.

    Example:

    foreach ($data_array as $key => &$param) {
        $key_number = $key + 1; //this won't work
        $key_name   = 'key' . $key_number;
        $stmt->bindParam($key_name, $param, PDO::PARAM_INT);
        var_dump($key_name);    // var dump 2
        var_dump($param);       // var dump 3
    }
    

提交回复
热议问题