PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi All;
I'm trying to find a good way to dynamically fill the "variable" arguments to a mysqli_bind_param() method. I have incoming $_GET vars, which if !empty, need to be passed to the bind_param. In code:
$myFirstVar = !empty($_GET['var']) ? $_GET['var'] : false;
$mySecondVar = !empty($_GET['var']) ? $_GET['var'] : false; (etc)
$aryFields[] = $myFirstVar ? "firstField = ?";
$aryFields[] = $mySecondVar ? "secondField = ?"; (etc)
/*I'm also using an array to hold the correspondint "?" types, which are then imploded into the first bind_param('string of types'), which results in some string like 'sissi' or whatever*/
//the statement is dynamically built if the ary members exist
$sql = "UPDATE myTable SET firstField = ?, secondField = ? WHERE some condition exists";
**this is the part that's thowing me***
/*I need to now call $db->bind_param('ississ', $arg1, $arg2) dynamically for only those fields that are to be updated. I'm looking for a good way to create $arg1, arg2 as necessary. I can create a single string easily enough from a dynamic array, but then there's a mismatch of types->arguments to replace.
What I really need (I think) is an array_func ??? that will seperate the array members into comma delimited string variable arguments (not a single string argument)*/
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Very raw thing.Untested. With little adjustments should work.
As you have probably already guessed you cannot tell apart blob type from string type.
So when you have to use blobs you should provide $fieldsType manually.
Thanks jmut; Much more elegant than what i came up with. I did play around with call_user_func_array() some but couldn't make it work! I'll do some testing with this and let you know how it goes. I suppose I was under the impression that one could only call a "user defined" function with it (which bind_param is not (or did you just not implement it?)). Again, thanks! If I can get this working properly, I'll add a method to my db class for it!