Link to home
Start Free TrialLog in
Avatar of Paul Konstanski
Paul KonstanskiFlag for United States of America

asked on

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).
<?php

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));

	mysqli_stmt_execute($stmt);
        $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?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paul Konstanski

ASKER

Gave me some good insight.