rarebit Posted November 4, 2013 Share Posted November 4, 2013 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 43That 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! Link to comment https://forums.phpfreaks.com/topic/283588-using-call_user_func_array-with-prepare-statement/ Share on other sites More sharing options...
rarebit Posted November 4, 2013 Author Share Posted November 4, 2013 After trying every version of array merge, it finally worked by changing this: $param[]=&$k; Link to comment https://forums.phpfreaks.com/topic/283588-using-call_user_func_array-with-prepare-statement/#findComment-1456907 Share on other sites More sharing options...
mac_gyver Posted November 4, 2013 Share Posted November 4, 2013 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. Link to comment https://forums.phpfreaks.com/topic/283588-using-call_user_func_array-with-prepare-statement/#findComment-1456908 Share on other sites More sharing options...
rarebit Posted November 4, 2013 Author Share Posted November 4, 2013 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 Link to comment https://forums.phpfreaks.com/topic/283588-using-call_user_func_array-with-prepare-statement/#findComment-1456917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.