Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:26:27+00:00 2026-05-22T23:26:27+00:00

I’m trying to dynamically bind mysql_stmt parameters and get the result in an associative

  • 0

I’m trying to dynamically bind mysql_stmt parameters and get the result in an associative array. I’ve found this post here on Stack Overflow where Amber posted an answer with the following code:

Original post:
How to make a proper mysqli extension class with prepared statements?

"Assuming you’re actually wanting to write your own version (as opposed to utilizing one of the existing libraries other answers have suggested – and those are good options, too)…

Here are a couple of functions which you may find it useful to examine. The first allows you to bind the results of a query to an associative array, and the second allows you to pass in two arrays, one an ordered array of keys and the other an associative array of data for those keys and have that data bound into a prepared statement:"

function stmt_bind_assoc (&$stmt, &$out) {
    $data = mysqli_stmt_result_metadata($stmt);
    $fields = array();
    $out = array();

$fields[0] = $stmt;
$count = 1;

while($field = mysqli_fetch_field($data)) {
    $fields[$count] = &$out[$field->name];
    $count++;
}
call_user_func_array(mysqli_stmt_bind_result, $fields);

}

function stmt_bind_params($stmt, $fields, $data) {
    // Dynamically build up the arguments for bind_param
    $paramstr = '';
    $params = array();
    foreach($fields as $key)
    {
        if(is_float($data[$key]))
            $paramstr .= 'd';
        elseif(is_int($data[$key]))
            $paramstr .= 'i';
        else
            $paramstr .= 's';
        $params[] = $data[$key];
    }
    array_unshift($params, $stmt, $paramstr);
    // and then call bind_param with the proper arguments
    call_user_func_array('mysqli_stmt_bind_param', $params);
}

I tried studying the code to understand what it does and I’ve made the second function work properly but I don’t know what I should do to be able to utilize the first function. How do I use it to retrieve an array similar to mysqli_result:: fetch_assoc()?

I want to be able to utilize the result in such a way like you used to do with:

while ($row = mysql_fetch_array($result)){
  echo $row['foo']." ".$row['bar'];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:26:28+00:00Added an answer on May 22, 2026 at 11:26 pm

    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 "<pre>";
        print_r($results);
        echo "</pre>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.