dtf24224 2015-05-23 11:01
浏览 281
已采纳

php错误发生警告:mysqli :: close():无法获取php中的mysqli错误[重复]

This question already has an answer here:

I have problem to make a code for connecting to DB and after that close the connections.

my DB class is below:

class DataBase {
    private $conn;
    private $info;
    private $db;

    public function __construct ($servername=DB_SERVER, $user=DB_USER, $pass=DB_PASS, $db=DB_DATABASE) {
                $this->db = $db;
                $this->conn = new mysqli($servername,$user,$pass, $this->db);


                if (!$this->conn)
                {
                    if  ($this->conn->connect_error) {
                        $this->info = "ERROR CODE=DB100: Connection failed <br> " . $conn->connect_error;
                        $this->close();
                        echo $this->getInfo();  
                    }
                    else {
                        $this->info = "ERROR CODE=DB101: Connection failed <br> ";
                        $this->close();
                        echo $this->getInfo();
                    }
                }
    }

    public function getInfo () {
        return $this->info;
    }

    // send sql to database
    public function sendQuery ($sql, $numr=null, $start=null) {

        if ($numr) {
            if ($start) 
                $sql .= " LIMIT $start, $numr";
            else 
                $sql .= " LIMIT $numr";
        }

                    $this->conn->set_charset("utf8");
        $result = $this->conn->query($sql);

        if ($result == true) {
                        return $result;
        }
        else {
                        $this->info = "ERROR CODE=DB102: Sql query not work <br>".$this->conn->error;
                        $this->close();
                        echo $this->getInfo();
                        return false;
        }

    }

            public function sendQueryNoneCahrSet ($sql, $numr=null, $start=null) {
                    if ($numr) {
                        if ($start) 
                                $sql .= " LIMIT $start, $numr";
                        else 
                                $sql .= " LIMIT $numr";
        }

        $result = $this->conn->query($sql);

        if ($result == true) {
            return $result;
        }
        else {
            $this->info = "ERROR CODE=DB103: Sql query not work <br>".$this->conn->error;
            $this->close();
            echo $this->getInfo();
            return false;
        }
            }


    // send sql query safe to avoid sql injection.
    public function getConn () {
                return $this->conn;
    }

    public function getDataBase (){
                return $this->db;
            }


    // This method close the connection
    public function close () {
                $this->conn->close();
    }

    function __destruct() {
                $this->close();
    }
}

and when I run this code:

           $con = @new DataBase();
           $r = $con->sendQuery($sql);
           $con->close();

I got this error:

Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\automasion\admin\protected\componets\database.php on line 102

program run correctly and set data into DB but this Error happen.

error point to:

$this->conn->close();

in my DB Connection code.

</div>
  • 写回答

1条回答 默认 最新

  • dongxian5735 2015-05-23 11:15
    关注

    In your constructor you created a mysqli instance :

    $this->conn = new mysqli($servername,$user,$pass, $this->db);
    

    but $this->db is null and has no value. you should change it to :

    $this->conn = new mysqli($servername,$user,$pass,$db);
    

    and another thing:

    function __destruct() {
                    $this->close();
        }
    

    this function runs when php wants to destroy your object, But you have closed your connection already!

    $con = @new DataBase();
    $r = $con->sendQuery("show tables");
    $con->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料