我有一个函数可以获取参数并将它们放在一个数组中,我需要将它们添加到mysqli_stmt_bind_param
的末尾,除了不确定如何执行此操作。
示例:
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;
}
是否有功能可以做到这一点或什么?我读过的任何建议或链接都不是懒散的。
也尝试了这个
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);
}
答案 0 :(得分:1)
你必须使用call_user_func_array:
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
这与做:
相同foobar('one', 'two');
注意: 将mysqli_stmt_bind_param()与call_user_func_array()结合使用时必须小心。请注意,mysqli_stmt_bind_param()需要通过引用传递参数,而call_user_func_array()可以接受可以表示引用或值的变量列表作为参数。