MySQLI使用call_user_func_array绑定params不起作用

时间:2015-10-24 05:41:27

标签: php mysqli bind

为什么不能正常工作?我试图根据用户发送GET数据动态绑定params。不确定它有什么问题。

    $bind_param = array();
    $array_param = array();
    $params = array();
    $i = 0;
    $bind_param[$i] = "i";
    $array_param[$i] = $_GET['p2'];

    $query = "SELECT * from point where fixture_id = ? ";

    if(isset($_GET['p1'])){
        $query .= "and user_id = ? ";
        $i++;
        $bind_param[$i] = "i";
        $array_param[$i] = $_GET['p1'];
    }

    $param_type = '';
    $n = count($bind_param);
    for($i = 0; $i < $n; $i++) {
      $param_type .= $bind_param[$i];
    }

    $params[] = $param_type;
    for($i = 0; $i < $n; $i++) {
      $params[] = $array_param[$i];
    }

    $stmt = $mysqli->prepare($query);

    call_user_func_array(array($stmt, 'bind_param'), $params);

    $stmt->execute();

错误:

PHP Warning:  Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given

1 个答案:

答案 0 :(得分:1)

尝试将引用放入数组而不是值(注意&符号):

$params[] = &$param_type;
for($i = 0; $i < $n; $i++) {
  $params[] = &$array_param[$i];
}