2015-05-04 92 views
2

我有一个函数可以获取参数并将它们放入一个数组中,我需要将它们添加到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

尝试implode()方法。 – TNC

+0

implode打开一个变量/字符串数组yes转到一个字符串...不工作队友 – EasyBB

回答

1

你必须使用call_user_func_array此:

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

这将是一样的做:

foobar('one', 'two'); 

this link

注意: 将mysqli_stmt_bind_param()与call_user_func_array()一起使用时必须小心。请注意,mysqli_stmt_bind_param()要求通过引用传递参数,而call_user_func_array()可以接受可以表示引用或值的变量列表作为参数。

+0

是的,我们需要做一个参考变量检查,否则它也会失败--_ – EasyBB

+0

我'我得到了一个答案,当我有时间为他人时,我会发布 – EasyBB