Jump to content

Adding Multiple Parameters to bind_param()


timothyarden

Recommended Posts

Hi Everyone,

I am using the bind_param() function and am building a function around it. I need an automated way of inserting a uncertain (unlimited) amount of parameters into this function. ie have the php code able to insert parameters into the bind_param() function on its own. So if it is given 3 parameters put them in as 

bind_param($param1,$param2,$param3)

 

or if given 5 parameters 

bind_param($param1,$param2,$param3,$param4,$param5)

I need a way of having the script insert them in for me. 

 

What I was thinking was having all of these parameters giving to a function above this as an array. I would then use a foreach loop to determine what type of value each value in this array is and create a string with the appropriate letters to have as the first parameter in the bind_param($value_types ...
Then I need a way of cycling through this array and adding how ever many values it has, as individual parameters, to the bind_param() function. 

 

Thanks in advance,

Timothy

 

Use call_user_func_array(). But bind_param wants references so

function foo(mysqli_stmt $stmt, array $args) {
$refargs = array("");
foreach ($args as $key => $value) {
$refargs[] =& $args[$key];
}
call_user_func_array(array($stmt, "bind_param"), $refargs);
}
That's a start.

Not sure what I am doing wrong and am not recieving any sql or php erros yet nothing is going into the database.

error_reporting(E_ALL);
ini_set('display_errors',1);

function test(mysqli_stmt $stmt, array $args) {
		$refargs = array();
		foreach ($args as $key => $value) {
			$refargs[] =& $args[$key];
		}
		call_user_func_array(array($stmt, "bind_param"), $refargs);
	}
	
	$mysqli = new mysqli(
		'localhost',
		'root', 
		'',
		'database_name'
	);
	
	$stmt = $mysqli -> stmt_init();
	$stmt -> prepare( "INSERT INTO users (`username`,`name`) VALUES(?,?)" );
	test($stmt, array('ss','testusername','testname'));
	$stmt -> execute();
	

	if($mysqli -> connect_error || $mysqli -> connect_errno || mysqli_stmt_error($stmt) || mysqli_stmt_errno($stmt)){ 
		echo 'MySQL Connection Error: ('.$mysqli -> connect_errno.')'.$mysqli -> connect_error ; 
		echo mysqli_stmt_errno($stmt).' - '.mysqli_stmt_error($stmt);
	} else {
		echo "Success";
	}
	
	$stmt -> close();
	

 

Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.