加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 无法在mysqli_stmt对象上使用call_user_func_array

发布时间:2020-12-13 13:42:16 所属栏目:PHP教程 来源:网络整理
导读:我正在为 MySQLi编写一个包装类.在那里,我正在编写一个函数来接受一个查询和可变数量的参数,我可以在其中调用mysqli_stmt :: bind_param.这是代码: ?php class DbHelper { .... public function Execute($query,$params){ $this-open(); # Opens a connecti
我正在为 MySQLi编写一个包装类.在那里,我正在编写一个函数来接受一个查询和可变数量的参数,我可以在其中调用mysqli_stmt :: bind_param.这是代码:
<?php
    class DbHelper {
        ....
        public function Execute($query,$params){
            $this->open(); # Opens a connection to the database using MySQLi API
            $stmt = $this->mysqli->prepare($query);
            try{
                $result = call_user_func_array(array($stmt,'bind_param'),$params);
            }
            catch(Exception $ex){
                # Handle Exception
            }
        }
        ....
    }
?>

这是我调用函数的方式:

<?php
    $db = new DbHelper();
    $params = array('i',$stateID);
    $result = $db->Execute('SELECT * FROM mst_cities WHERE State_ID = ?',$params);    
?>

当我运行代码时,我得到一个警告:
警告:参数2到mysqli_stmt :: bind_param()应该是一个引用,值在……中给出

我该怎么办?

从 docs:

Note:

Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference,whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.

评论中有一些解决方法,例如see this:

call_user_func_array(array($stmt,refValues($params));

function refValues($arr)
{
    if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
    {
        $refs = array();
        foreach($arr as $key => $value)
            $refs[$key] = &$arr[$key];
        return $refs;
    }
    return $arr;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读