Mysqli-> fetch_row问题

时间:2013-01-26 13:57:23

标签: php mysql mysqli

我从未在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]

但是我知道查询是好的,因为我可以回应它并直接在db上使用它给我很好的结果。我有一种感觉,我正在使用“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

2 个答案:

答案 0 :(得分:2)

$ this-&gt; mysqli未初始化

答案 1 :(得分: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