timothyarden Posted April 7, 2013 Share Posted April 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/ Share on other sites More sharing options...
Solution requinix Posted April 7, 2013 Solution Share Posted April 7, 2013 (edited) 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 April 7, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/#findComment-1423348 Share on other sites More sharing options...
timothyarden Posted April 7, 2013 Author Share Posted April 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/#findComment-1423349 Share on other sites More sharing options...
timothyarden Posted April 7, 2013 Author Share Posted April 7, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/#findComment-1423352 Share on other sites More sharing options...
requinix Posted April 7, 2013 Share Posted April 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/#findComment-1423373 Share on other sites More sharing options...
timothyarden Posted April 7, 2013 Author Share Posted April 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276628-adding-multiple-parameters-to-bind_param/#findComment-1423376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.