mysqli - mysqli_stmt :: bind_param()应该是一个参考

时间:2012-10-30 08:09:42

标签: php mysqli

这是我的班级代码:

class DAL
{

public function paramtypez($val)
{
        $types = '';                        //initial sting with types
        foreach($val as $para) 
        {     
            if(is_int($para)) {
                $types .= 'i';              //integer
            } elseif (is_float($para)) {
                $types .= 'd';              //double
            } elseif (is_string($para)) {
                $types .= 's';              //string
            } else {
                $types .= 'b';              //blob and unknown
            }
        }
        return $types;
}

 public function execsql($query,$param)
{
    $conn=new mysqli("127.0.0.1","root","","sample");

    if($row=$conn->prepare($query))
    {
        array_unshift($param,array(DAL::paramtypez($param)));
        call_user_func_array(array($row, 'bind_param'), $param);

        $row->execute();
        return true;
    }
    else
    return false;

}
}

并使用此类:

$vars = new DAL();
$vars->execsql('insert into tesst (name,family,age) values(?,?,?)',array('mori','gre','25'));

但是这回复了我这个错误:

 Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ..\htdocs\inc.php on line 30

如何修复此错误? 谢谢你的帮助

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为当bind_param期望实际引用时,您正在给出一个数组:

试试看是否有效:

class DAL
{

    public function paramtypez($val)
    {
            $types = '';                        //initial sting with types
            foreach($val as $para) 
            {     
                if(is_int($para)) {
                    $types .= 'i';              //integer
                } elseif (is_float($para)) {
                    $types .= 'd';              //double
                } elseif (is_string($para)) {
                    $types .= 's';              //string
                } else {
                    $types .= 'b';              //blob and unknown
                }
            }
            return $types;
    }

    public function execsql($query,$param)
    {

        $conn=new mysqli("127.0.0.1","root","","sample");

        if($row=$conn->prepare($query))
        {
            array_unshift($param,array(DAL::paramtypez($param)));
            call_user_func_array(array($row, 'bind_param'), $this->makeValuesReferenced($param));

            $row->execute();
            return true;
        }
        else
        return false;

    }


    protected function makeValuesReferenced(&$arr)
    {
        $refs = array();
        foreach($arr as $key => $value)
            $refs[$key] = &$arr[$key];
        return $refs;
    }

}

答案 1 :(得分:2)

在将它传递给call_user_func_array之前,

$param看起来就像这样:

[['ssi'],'mori','gre','25']

这是不正确的。你应该改变这个:

array_unshift($param,array(DAL::paramtypez($param)));

对此:

array_unshift($param,DAL::paramtypez($param));