发送一个参数数组bind_param

问题描述:

我有参数发送到阵列中的准备语句,我使用call_user_func_array并使用它,因为call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $params_fixed)),其中$types包含的类型和$params_fixed包含的参数。发送一个参数数组bind_param

我跑,并得到Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ...,我搜索了这个错误的错误和答案是通过参考发送参数,所以我增加了$params_fixed参数之前的符号但是现在我得到的错误Fatal error: Call-time pass-by-reference has been removed in ...

我该如何解决这个问题?我在这里错过了什么?

注:学习我不得不使用call_user_func_array之前,我是用它作为这样$stmt->bind_param($types, ...$params_fixed)

注2:下面是用于填充数组发送

$params_fixed = array(); 
    $types = ""; 
    if($param_count > 0) { 
     foreach($params as $param) { 
     switch(gettype($param)) { 
      case "boolean": 
      $types = $types . "i"; 
      $params_fixed[] = $param ? 1 : 0; 
      break; 
      case "integer": 
      $types = $types . "i"; 
      $params_fixed[] = &$param; 
      break; 
      case "double": 
      $types = $types . "d"; 
      $params_fixed[] = &$param; 
      break; 
      case "string": 
      $types = $types . "s"; 
      $params_fixed[] = &$param; 
      break; 
      default: 
      $types = $types . "s"; 
      $params_fixed[] = null; 
      break; 
     } 
     } 
    } 

注3的代码:下面是在问题

public function query($sql, ...$params) { 
    $param_num_sql = substr_count($sql, "?"); 
    $param_count = count($params); 

    if($param_num_sql != $param_count) { 
    $this->error = 'parameters don\'t match'; 
    return null; 
    } 

    $params_fixed = array(); 
    $types = ""; 
    if($param_count > 0) { 
    foreach($params as $param) { 
     $types = $types . "s"; 
     $params_fixed[] = &$param; 

     // switch(gettype($param)) { 
     // case "boolean": 
     //  $types = $types . "i"; 
     //  $params_fixed[] = $param ? 1 : 0; 
     //  break; 
     // case "integer": 
     //  $types = $types . "i"; 
     //  $params_fixed[] = $param; 
     //  break; 
     // case "double": 
     //  $types = $types . "d"; 
     //  $params_fixed[] = $param; 
     //  break; 
     // case "string": 
     //  $types = $types . "s"; 
     //  $params_fixed[] = $param; 
     //  break; 
     // default: 
     //  $types = $types . "s"; 
     //  $params_fixed[] = null; 
     //  break; 
     // } 
    } 
    } 

    if($param_num_sql == 0) { 
    $result = $this->conn->query($sql); 
    } else { 
    $stmt = $this->conn->prepare($sql); 

    //call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $params_fixed)); 

    //if(!$stmt->bind_param($types, ...$params_fixed)) { 


echo "<br/>types: $types<br/>"; 
echo '<br/>'; 
print_r($params_fixed); 
echo '<br/>'; 


    if(!call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $params_fixed))) { 
     // an error occurred 
    } 

    $stmt->execute(); 

    $result = $stmt->get_result(); 

    $stmt->close(); 
    } 

    if($result != null && $result->num_rows > 0) 
    return $result->fetch_all(); 
    else 
    return null; 
} 

和下面的代码是代码调用此方法

$dbcon->query($query, $fname, $mname, $lname, $dob, $mobile, $home, $email, $area, $city, $street, $bldg, $floor, $car_capacity, $status, $prefer_act, $volunteer_days, $backup_days);

+0

http://*.com/questions/5100046/how-to-bind- mysqli的绑定 - PARAM论点,动态的,PHP – hassan

+1

可能重复[如何在PHP动态绑定的mysqli绑定\ _Param参数?](http://*.com/questions/5100046/how-to-bind- mysqli的绑定-PARAM-参数动态地功能于PHP) – trincot

尝试这样

 $mysqli = new mysqli('localhost', 'root','','mydb'); 

     $stmt=$mysqli->prepare("select * from blog where id=? and date=?"); 
      $title='1'; 
      $text='2016-04-07'; 
     call_user_func_array(array($stmt, "bind_param"),array_merge(array('ss'),array(&$title,&$text))); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     print_r($result->fetch_array()); 
     echo $stmt->error; 

OK,如果你有从阵列产生的参数

 $mysqli = new mysqli('localhost', 'root','','jobspace'); 

     $stmt=$mysqli->prepare("select * from listings where listing_type_sid=? and user_sid=?"); 
      $title='6'; 
      $text='8'; 
      $arr=array($title,$text); 
      foreach($arr as &$ar){ 
       $new[]=&$ar; 
      } 
     $types = implode(array_fill(0,count($arr),'s'));   
     call_user_func_array(array($stmt, "bind_param"),array_merge(array($types),$new)); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     print_r($result->fetch_array()); 
     echo $stmt->error; 
+0

在填充我的'$ params_fixed'数组时,我通过引用存储了变量,并且我得到了一个存储在我的表中的记录,但所有记录具有相同的值“10”,日期为“0000-00-00” –

+0

我在 –

+0

之上添加了填充我的参数变量的代码,打印您的$ params_fixed和$类型,并在将它发送到call_user_func_array()之前看到它具有正确的值。 –