发送参数数组到bind_param [英] Sending an array of parameters to bind_param

查看:86
本文介绍了发送参数数组到bind_param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要发送到数组中准备好的语句的参数,我正在使用call_user_func_array并将其用作call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $params_fixed)),其中$types包含类型,而$params_fixed包含参数.

I have the parameters to send to a prepared statement in an array, I am using call_user_func_array and using it as such call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $params_fixed)), where $types contains the types and $params_fixed contains the parameters.

我运行它并得到错误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 ....

I ran it and got the error Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ..., I searched for this error and an answer was to send the parameters by reference so I added an ampersand before the $params_fixed parameter however now I get the error Fatal error: Call-time pass-by-reference has been removed in ....

如何解决此问题?我在这里想念什么?

How can I resolve this issue? What am I missing here?

注意:在学习之前,我必须先使用call_user_func_array,我以前是这样使用它的$stmt->bind_param($types, ...$params_fixed)

NOTE: Before learning I had to use call_user_func_array, I was using it as such $stmt->bind_param($types, ...$params_fixed)

注意2:以下是用于填充要发送的数组的代码

NOTE 2: below is the code for filling the array to send

    $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:下面是有问题的代码

NOTE 3: below is the code in question

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

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

and below is the code calling this method

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

推荐答案

尝试这样

        $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 if you have parameters coming from the array

        $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;

这篇关于发送参数数组到bind_param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆