Hi! I have a problem with binding parameters dynamically in my mysqli_stmt objects. As I don't know how many parameters which will be put into this function I must bind the parameters dynamically. Though I cannot understand what's wrong with this method:

public function execute($unprp_stmt, $data){
   //Prepare the stmt
   $stmt = $this->con->prepare($unprp_stmt);
   //Get dynamically built parameter string for the bind_param method. E.g. 'issis'
   $paramstr = $this->get_paramstr($data);
   // Dynamically build up the arguments for bind_param
   $params = array();
   array_unshift($params, $stmt, $data, $paramstr);
   // and then call bind_param with the proper arguments
   call_user_func_array('mysqli_stmt_bind_param', $params);

Out of this I get the following error:

Output of print_r:
Array ( [0] => mysqli_stmt Object ( [affected_rows] => -1 [insert_id] => 0 [num_rows] => 0 [param_count] => 1 [field_count] => 6 [errno] => 0 [error] => [sqlstate] => 00000 [id] => 1 ) [1] => i [2] => 1 ) 

And the following error:
[B]Warning:[/B] Parameter 3 to mysqli_stmt_bind_param() expected to be a reference, value given in C:\xampp\htdocs\HTSIBWORT\gw_fetcher.php on line 27

as we can see in the output from print_r is the value of parameter 3 = 1.

Why doesn't this work? Please help me out!

7 Years
Discussion Span
Last Post by gunnarflax

Could someone please help me by examining closely how these methods work? I found them when trying to solve this problem:

(original post)

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];
    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)
            $paramstr .= 'd';
            $paramstr .= 'i';
            $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);

What I don't get is what the first class is doing. How do I utilize the result from it?

Btw, I found a solution to the earlier problem in the comments on this page:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.