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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来