Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Beforehand, I had a method which allows the user to specify a mysqli connection, a query string with '?' to avoid SQL injection, and an array of parameters. It would then dynamically bind the params and output the result as an associative array.

Now I've moved the parameter binding part out into a separate method as I'm adding more implementations for it. The issue now is:

call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object

This is the method:
PHP
private static function bind_dynamic_params(&$stmt, $params)
        {
                // generate the params type string
                $param_types = str_repeat('s', count($params));

                // set the type string as the first parameter
                $param_bind_names[] = $param_types;

                // iterate through params
                for($i = 0; $i < count($params); $i++)
                {
                        // Create a variable name
                        $param_bind_name = 'bind' . $i;

                        // add the parameter to the variable
                        $$param_bind_name = $params[$i];

                        // associate the variable as an element in the array
                        $param_bind_names[] = &$$param_bind_name;
                }

                // bind params
                call_user_func_array(array($stmt, 'bind_param'), $param_bind_names);
        }

I'm looking to pass the $stmt object by reference so call_user_func_array can be called on it, while the original method can use this altered object.

The way I'm trying to do this is:

1. $conn = new mysqli(...); $stmt = $conn->prepare($query);
2. bind_dynamic_params($stmt, $params);
3. Continue using $stmt now, such as ->execute() or ->close().

What I have tried:

Passing by reference, looking online for around half an hour
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900