0

我从未在 php 中使用过新的 mysqli 类,因为旧的 mysql 接口总是足够好,但是我正在尝试更新一些旧代码以使用 mysqli,但事情进展并不顺利,我收到以下错误:

[error] [client 127.0.0.1] PHP Fatal error:  Call to a member function fetch_row() on a non-object in  [location of file]

但是我知道查询很好,因为我可以回显它并直接在数据库上使用它来给我很好的结果。我有一种感觉,我错误地使用了“fetch_row()。

有人可以建议我做错了什么吗?

/// class setup stuff
public function query($query,$sanitize=TRUE)
    {       
        if($sanitize!==FALSE)
        {
            $query=$this->mysqli->escape_string($query);
        }
        echo $query;
        $this->mysqli->query($query);
        echo $this->mysqli->error;
        //$this->mysqli->free();
    }    
public function select($table,$what,$where,$orderby=FALSE,$order=FALSE,$limits=FALSE,$sanitize=TRUE) //this is really simple and very limited
        {
            //process the select query and send to query method
            $query = "SELECT $what FROM $table WHERE ";
            $i = 0;
            foreach($where as $key => $value)
            {
                $key    = $sanitize?$this->mysqli->escape_string($key):$key;
                $value  = $sanitize?$this->mysqli->escape_string($value):$value;
                $query  .= "$key='$value' ";
                $i++;
                if($i<count($where))
                {
                    $query .= "AND ";
                }
            }
            // check the max rowcount 
            $this->query($query,FALSE);
            $result = $this->mysqli->use_result(); //<--this is the issue
4

2 回答 2

2

$this->mysqli 未初始化

于 2013-01-26T13:59:11.907 回答
0

感谢@scones 的建议,我在初始化连接后对其进行了测试,发现问题是“use_result”以及我没有在查询函数中返回结果的事实。这是工作版本通知$result = $this->query($query,FALSE);

public function query($query,$sanitize=TRUE)
{       
    if($sanitize!==FALSE)
    {
        $query=$this->mysqli->escape_string($query);
    }
    echo $query;
    if($this->mysqli->error)
    {
        echo $this->mysqli->error;
        exit;
    }
    else
    {
        return $this->mysqli->query($query); //<-- needed this
    }
    //$this->mysqli->free();
}

public function select($table,$what,$where,$orderby=FALSE,$order=FALSE,$limits=FALSE,$sanitize=TRUE) //this is really simple and very limited
{
    //process the select query and send to query method
    $query = "SELECT $what FROM $table WHERE ";
    $i = 0;
    foreach($where as $key => $value)
    {
        $key    = $sanitize?$this->mysqli->escape_string($key):$key;
        $value  = $sanitize?$this->mysqli->escape_string($value):$value;
        $query  .= "$key='$value' ";
        $i++;
        if($i<count($where))
        {
            $query .= "AND ";
        }
    }
    $result = $this->query($query,FALSE);  //<-- works now
于 2013-01-26T14:13:57.317 回答