Jump to content


Photo

Adding Multiple Parameters to bind_param()

mysqli bind_param parameters automatically

Best Answer requinix, 06 April 2013 - 08:43 PM

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. Go to the full post


  • Please log in to reply
5 replies to this topic

#1 timothyarden

timothyarden

    Advanced Member

  • Members
  • PipPipPip
  • 149 posts

Posted 06 April 2013 - 08:37 PM

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

 



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,239 posts
  • LocationWA

Posted 06 April 2013 - 08:43 PM   Best Answer

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.

Edited by requinix, 06 April 2013 - 08:43 PM.


#3 timothyarden

timothyarden

    Advanced Member

  • Members
  • PipPipPip
  • 149 posts

Posted 06 April 2013 - 08:49 PM

Okay, thanks heaps Requinix, Will read over the call_user_func_array documentation and try to modify your code to suit my needs. Thanks again for your help. Will keep the topic open in case I need any more help.



#4 timothyarden

timothyarden

    Advanced Member

  • Members
  • PipPipPip
  • 149 posts

Posted 06 April 2013 - 09:52 PM

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?



#5 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,239 posts
  • LocationWA

Posted 07 April 2013 - 06:30 AM

There should be some output of some kind. If not then you may have a parse error somewhere, but if it's in that code then I don't see it.

#6 timothyarden

timothyarden

    Advanced Member

  • Members
  • PipPipPip
  • 149 posts

Posted 07 April 2013 - 06:48 AM

Didn't change the code at all, just restarted XAMPP and it started working - so not sure what was going on. Thanks heaps for your help.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com