MySQLI binding params using call_user_func_array

后端 未结 7 2302
别跟我提以往
别跟我提以往 2020-11-30 09:01

Please see below my code. I am attempting to bind an array of paramenters to my prepared statement. I\'ve been looking around on the web and can see I have to use call_user_

7条回答
  •  無奈伤痛
    2020-11-30 09:50

    It must be like this:

    //connect
    $mysqli = new mysqli($host, $user, $password, $db_name);
    
    //prepare
    $stmt = $mysqli->prepare("SELECT * FROM the_table WHERE field1= ? AND Field2= ?");
    
    //Binding parameters. Types: s = string, i = integer, d = double,  b = blob
    $params= array("ss","string_1","string_2");
    
    //now we need to add references
    $tmp = array();
    foreach($params as $key => $value) $tmp[$key] = &$params[$key];
    // now us the new array
    call_user_func_array(array($stmt, 'bind_param'), $tmp);
    
    $stmt->execute();
    
    /* Fetch result to array */
    $res = $stmt->get_result();
    while($row = $res->fetch_array(MYSQLI_ASSOC)) {
      $a_data[]=$row;
    }
    print_r($a_data);
    
    $stmt->close();
    

提交回复
热议问题