简体   繁体   English

关于PHP的准备声明

[英]About PHP prepared statement

I'm working around for an easy function to execute prepared statement. 我正在努力实现执行准备好的语句的简单功能。 Now I get a problem: 现在我有一个问题:

class mysqli_extend extends \mysqli{
function pquery(){
    $input  = func_get_args();
    if ($stmt   = parent::prepare($input[0])){
        $input  = array_slice($input,1);
        $index = count($input);
        if ($index){ //non-0 == true, 0 == false
            $typestr = '';
            while ($ele = current($input)){
                $type   = gettype($ele);
                switch($type) {
                    case "integer": $typestr .= 'i';    break;
                    case "double":  $typestr .= 'd';    break;
                    case "string":  $typestr .= 's';    break;
                    default:        $typestr .= 'b';    break;
                }
                next($input);
            }
            array_unshift($input,$typestr);

            //output to console checking array content
            while ($ele = current($input)){
                echo key($input) . ":" . $ele . "\t";
                next($input);
            }

            call_user_func_array(array($stmt,'bind_Param'),$input);
        }
        $stmt->execute();
    }
    return true;
}
}

and this is the command to execute: 这是要执行的命令:

$test = $mysqli_extend->pquery('UPDATE user_list SET upriv = ? WHERE uname =?','moderator','admin');

while echo statement gives me a correct content of array"0:ss 1:moderator 2:admin" I get error expectedly with: "Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference" 虽然echo语句为我提供了数组的正确内容“ 0:ss 1:moderator 2:admin”,但我收到以下错误:“警告:mysqli_stmt :: bind_param()的参数2应该是引用”

since for the newest php version, it has the problem "Fatal error: Call-time pass-by-reference has been removed" 因为对于最新的php版本,它具有问题“致命错误:调用时传递引用已被删除”

May I ask whether there is any workaround for this problem? 请问是否有解决此问题的方法?

thanks to ryan vincent, following the link i get the temporary work around although I didn't expect this to work in future: 感谢ryan vincent,通过该链接我可以进行临时工作,尽管我没想到将来会这样:

foreach($input as $k => &$arg){
    $Args[$k] = &$arg;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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