Dynamically bind mysqli_stmt parameters and then bind result (PHP)

后端 未结 4 1837
忘了有多久
忘了有多久 2020-12-03 12:27

I\'m trying to dynamically bind mysql_stmt parameters and get the result in an associative array. I\'ve found this post here on stackoverflow where Amber posted an answer wi

4条回答
  •  借酒劲吻你
    2020-12-03 13:08

    Okay, here is a way to do it:

    Edited, to fix bug when fetching multiple rows

    $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?";
    $params = array('Australia','Victoria');
    
    /*
        In my real app the below code is wrapped up in a class 
        But this is just for example's sake.
        You could easily throw it in a function or class
    */
    
    // This will loop through params, and generate types. e.g. 'ss'
    $types = '';                        
    foreach($params as $param) {        
        if(is_int($param)) {
            $types .= 'i';              //integer
        } elseif (is_float($param)) {
            $types .= 'd';              //double
        } elseif (is_string($param)) {
            $types .= 's';              //string
        } else {
            $types .= 'b';              //blob and unknown
        }
    }
    array_unshift($params, $types);
    
    // Start stmt
    $query = $this->connection->stmt_init(); // $this->connection is the mysqli connection instance
    if($query->prepare($sql)) {
    
        // Bind Params
        call_user_func_array(array($query,'bind_param'),$params);
    
        $query->execute(); 
    
        // Get metadata for field names
        $meta = $query->result_metadata();
    
        // initialise some empty arrays
        $fields = $results = array();
    
        // This is the tricky bit dynamically creating an array of variables to use
        // to bind the results
        while ($field = $meta->fetch_field()) { 
            $var = $field->name; 
            $$var = null; 
            $fields[$var] = &$$var; 
        }
    
    
        $fieldCount = count($fieldNames);
    
    // Bind Results                                     
    call_user_func_array(array($query,'bind_result'),$fields);
    
    $i=0;
    while ($query->fetch()){
        for($l=0;$l<$fieldCount;$l++) $results[$i][$fieldNames[$l]] = $fields[$fieldNames[$l]];
        $i++;
    }
    
        $query->close();
    
        // And now we have a beautiful
        // array of results, just like
        //fetch_assoc
        echo "
    ";
        print_r($results);
        echo "
    "; }

提交回复
热议问题