Jump to content

using call_user_func_array() with prepare statement


Go to solution Solved by rarebit,

Recommended Posts

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.