简体   繁体   中英

Pass Variable as reference to mysqli_stmt::bind_param()

I'm trying to dinamically pass some variables to mysqli_stmt::bind_param() , but I'm stuck (probably is there a problem even with 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']) )){
[...]

This is the error:

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

All the varibles has been decalered before this piece of code and are correct. I have tried to add the & before the variable(example: $merge['val'][]=&$id; ), but it says that is an unexpected character ( I expected this error, 'cause I have no idea of what I was doing)
Thanks in advance

Check out the code in the better_mysqli.php class I wrote that extends mysqli. It does exactly what you are trying to do: better_mysqli.php

This page shows it's basic usage: basic_usage.php

This page shows detailed usage: detailed_usage.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM