传递变量作为对mysqli_stmt :: bind_param()的引用

时间:2013-07-24 18:26:38

标签: php arrays mysqli

我正试图将一些变量传递给mysqli_stmt::bind_param(),但我被卡住了(即使call_user_func_array也可能存在问题):

[..]
else if($_SESSION['status']==2){
        $merge=array('type'=>array(),'val'=>array());
        $tail=array();
        if($id!=''){
            $tail[]='`user_id`=?';
            $merge['type'][]='i';
            $merge['val'][]=$id;
        }
        if($enid!=''){
            $tail[]='`ref_id`=?';
            $merge['type'][]='s';
            $merge['val'][]=$enid;
        }
        if($tit!=''){
            $tail[]='`title`=?';
            $merge['type'][]='s';
            $merge['val'][]=$tit;
        }
        if($dep!=''){
            $tail[]='`department_id`=?';
            $merge['type'][]='i';
            $merge['val'][]=$dep;
        }
        if($opid!=''){
            $tail[]='`operator_id`=?';
            $merge['type'][]='i';
            $merge['val'][]=$opid;
        }
        if($op!=''){
            $tail[]='`operator_id` IN (SELECT `id` FROM '.$SupportUserTable.' WHERE `name`=? AND 0!=`status`)';
            $merge['type'][]='s';
            $merge['val'][]=$op;
        }
        if($from!=''){
            $tail[]='`created_time` <= ?';
            $merge['type'][]='s';
            $merge['val'][]=$from;
        }
        if($to!=''){
            $tail[]='`created_time` >= ?';
            $merge['type'][]='s';
            $merge['val'][]=$to;
        }
        if($usmail!=''){
            $tail[]='(user_id=(SELECT `id` FROM '.$SupportUserTable.' WHERE `mail`=? LIMIT 1) OR operator_id=(SELECT `id` FROM '.$SupportUserTable.' WHERE `mail`=? LIMIT 1))';
            $merge['type'][]='ss';
            $merge['val'][]=$usmail.','.$usmail;
        }
        $query.=implode(' AND ',$tail);
    }
    $prepared = $stmt->prepare($query);
    if($prepared){
        if(call_user_func_array(array($stmt, "bind_param"), array(implode('',$merge['type']), $merge['val']) )){
[...]

这是错误:

PHP Warning:  Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\xampp\htdocs\php\function.php on line 2165

在这段代码之前所有的变量都已经过去了并且是正确的。 我试图在变量之前添加&(例如:$merge['val'][]=&$id;),但它说这是一个意想不到的字符(我预料到这个错误,因为我不知道我在做什么)
提前致谢

1 个答案:

答案 0 :(得分:1)

查看我写的扩展mysqli的better_mysqli.php类中的代码。它完全符合您的要求:better_mysqli.php

此页面显示了它的基本用法:basic_usage.php

此页面显示详细用法:detailed_usage.php