EasyBB
EasyBB

Reputation: 6574

turn an array of arguments into a comma serparated list of arguments

I have a function that gets arguments and places them in an array, and I need to add them to the end of mysqli_stmt_bind_param, except not sure how to do this.

Example:

if($stmt = mysqli_prepare(self::$conn,$sql)) {
                    /*
                        i   corresponding variable has type integer
                        d   corresponding variable has type double
                        s   corresponding variable has type string
                        b   corresponding variable is a blob and will be sent in packets
                    */
                $types = "";
                $vals = array();
                foreach($prepared as $type=>$param) {
                    if(strlen($type) > 1) {
                        self::$errors[]="Only one type is allowed";
                        return false;
                    } else {
                        $types.= $type;
                        $vals[] = $param;
                    }
                    mysqli_stmt_bind_param($stmt,$types,$param);//need param to be a multiple argument separated by commas of course!???
                }
                $passfail = mysqli_stmt_execute($stmt);
                return $passfail;
            }

Is there already a function that can do this or what? Any suggestions or links I'll read not a slacker.

tried this as well

foreach($prepared as $type=>$param) {
                    if(strlen($type) > 1) {
                        self::$errors[]="Only one type is allowed";
                        return false;
                    } else {
                        $types.= $type;
                        $vals[] = $param;
                    }
                    array_unshift($vals,$types);
                    array_unshift($vals,$stmt);
                    call_user_func_array("mysqli_stmt_bind_param",$vals);
                }

Upvotes: 2

Views: 63

Answers (1)

Bidhan Bhattarai
Bidhan Bhattarai

Reputation: 1060

You have to use call_user_func_array for this:

// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));

This would be the same as doing:

foobar('one', 'two');

Read this link

Note: Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference, whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.

Upvotes: 1

Related Questions