Jump to content


Photo

using call_user_func_array() with prepare statement

call_user_func_array

Best Answer rarebit, 04 November 2013 - 01:46 PM

After trying every version of array merge, it finally worked by changing this:

$param[]=&$k;
Go to the full post


  • Please log in to reply
3 replies to this topic

#1 rarebit

rarebit

    Advanced Member

  • Members
  • PipPipPip
  • 1,002 posts

Posted 04 November 2013 - 12:51 PM

Hi, i'm getting the following error when executing the following code. The count should be right
 

Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables in ... line 43

That line may now be 39 ( call_user_func_array() )
I've tried hard coding the types too (so no extra comma)! And without array_values(), plus other things... and when I print each set of params out they are right
 
if(isset($_POST['order'])){
	print "Order<br />";
	
	$param=array();
	$params="";
	$params2="";
	foreach($_POST as $k=>$v){
		$pos = strpos($k, 'sku');
		if($pos!==false&&$pos==0){
			$param[]=$k;
			$params.="s";
			$params2.="?,";
		}
	}
	
	if(count($param)>0){
		print "Selecting<br />";
		if($stmt = $db->prepare("SELECT sku, title, info, quant, cost FROM ".$db_table_prefix."product WHERE sku IN (".$params2.")")) {
			call_user_func_array('mysqli_stmt_bind_param', array($stmt, $params, array_values(&$param)));
			
			$stmt->execute();
Many thanks!

Edited by rarebit, 04 November 2013 - 12:52 PM.


#2 rarebit

rarebit

    Advanced Member

  • Members
  • PipPipPip
  • 1,002 posts

Posted 04 November 2013 - 01:46 PM   Best Answer

After trying every version of array merge, it finally worked by changing this:
$param[]=&$k;


#3 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,568 posts

Posted 04 November 2013 - 02:24 PM

you also have an extra comma after the last place holder ?, that is or should be producing an error.

 

you should try to make a general purpose prepared query function (or a class) that you can reuse, instead of repeating the body of that code each time you are running a query.

 

if you had a function that accepted the $db, the completed sql statement with the place holders already in it, the list of 'sssiiisis...' parameters, and an array of the data values, all you would need to do in your main code is form the correct parameters and call your function.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#4 rarebit

rarebit

    Advanced Member

  • Members
  • PipPipPip
  • 1,002 posts

Posted 04 November 2013 - 03:24 PM

In reality I had another issue with that reference, they all came through as the same, so I did basically the same but to a temporary array then copied it (may have time to find a better method later), also as you said the trailing comma err'd on me, so here's a quick amendment.

	$params2=substr($params2, 0, -1);
	$param=array();	//	need to copy because need references!!! grrr
	for($i=0;$i<count($aa);$i++){
		$param[]=&$aa[$i];
	}
I generate the params and types on the fly so that any number of products may be added at once.

I will be banging these (+others) into functions later, but / or how do you mean? (I saw some class examples on php.net but i'm in a rush so just moved on, but I do prefer to optimise wherever possible!)

Cheers




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