Using PHP mysqli_stmt_bind_param with Variable Arguments

I need some help figuring out how to do something with the "mysqli_stmt_bind_param" process. I'm trying to create a function that allows me to do a database insert. I want this function to have the flexibility of being able to pass a variable amount of variables.

The approach I'm taking is to declare the variables as an array that is passed through the function. I want the function to split out the array and create the appropriate mysqli_stmt_bind_param arguments.  Here's what I have so far.  (Note some of this is abbreviated for clarity).

function refValues($arr){ 
    if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+ 
        $refs = array(); 
        foreach($arr as $key => $value) 
            $refs[$key] = &$arr[$key]; 
        return $refs; 
    return $arr; 

function doInsert($query, $types, $values) 
	DEFINE ('DB_HOST', $stngs->dbServer);
	DEFINE ('DB_USER', $stngs->dbUsername);
	DEFINE ('DB_PASSWORD', $stngs->dbPassword);
	DEFINE ('DB_NAME', $stngs->dbDatabase);

	$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);  // the constants are 

        $stmt = mysqli_prepare($dbc, $query);
        # if I print out the statment I get this so I know the prepare is working
        # mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 3 [field_count] => 0 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 1 )
        call_user_func_array(array($stmt, "mysqli_stmt_bind_param"),refValues($values));

        $affected_rows = mysqli_stmt_affected_rows($stmt);

       die ("Arrected Rows are: [".$affected_rows."]");


// this is an example of an insert statement with three values

$query = "INSERT INTO roger (`timestamp`, `key`, `value`) VALUES (?, ?, ?)";  // the bind query statment
$types = "iss";  // the types
$values = array("timestamp" => mktime(), "key" => "toast", "value" => date("Y-m-d"));

doInsert($query, $types, $values);

Open in new window

When I run this script, I get the DB connected okay (I tested with a select that worked) but I get this error message.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'mysqli_stmt' does not have a method 'mysqli_stmt_bind_param'

And the affected rows is [0] with no entry into the database.

What am I doing wrong?
Paul KonstanskiProject SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Take a look at the manual for call_user_func_array
This is your implementation

call_user_func_array(array($stmt, "mysqli_stmt_bind_param"),refValues($values));

Open in new window

The function expects the name of a function as the first parameter and you are passing it an array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve BinkCommented:
Your call is probably correct.  I think it's the name that is wrong.  

If you see Julian's link to call_user_func_array(), follow the link for "callable", where you'll see:

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

[[ ------------------ emphasis added ------------------ ]]
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
[[ ------------------ /emphasis added ------------------ ]]

The mysqli_stmt_bind_param name is the functional equivalent of what you want.  Change the name to simply "bind_param".
you shouls look at the manual at -

and then READ the user comments at the bottom, they give several examples for have an array into the bind param thing, , You shod have PHP version at least 5.4, if so, try and use the PHP ReflectionClass() thing -
$db     = new mysqli("localhost","root","","tests");
$res    = $db->prepare("INSERT INTO test SET foo=?,bar=?");
$refArr = array("si","hello",42);
$ref    = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod("bind_param");

Open in new window

Paul KonstanskiProject SpecialistAuthor Commented:
Gave me some good insight.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.