【问题标题】:Mysqli->fetch_row issuemysqli->fetch_row 问题
【发布时间】:2013-01-26 13:57:22
【问题描述】:

我从来没有在 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

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    $this->mysqli 未初始化

    【讨论】:

    • 我在我的 __construct 函数中使用它: $this->mysqli = new mysqli($this->host,$this->user,$this->pass,$this->name,$这个->端口); if (mysqli_connect_errno()) { printf("连接失败:%s\n", $this->mysqli->error );出口(); }
    • 如果成功的话,你可能想在新行之后测试一下
    • 我可以把整个班级都丢在这个问题上,但对人们来说可能有点乏味?
    • 好吧,在再次查看您的代码之后:这两个函数不使用fetch_row()。你的错误在别处
    • 我同意烤饼的说法,即错误在其他地方。也许您可以使用 PHP 的 debug_print_backtrace() 来查找脚本到达 fetch_row() 之前调用的内容;
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多