title: Why Warning: mysqli_query(): Couldn't fetch mysqli?
body: <p>I don't understand why I remove code in line 14($this->close();), it don't have error but i don't remove it then it warning mysqli_query(): Couldn't fetch mysqli. It is at the end construct ???
  My error: <a href="http://i.stack.imgur.com/H8V9u.png" rel="nofollow">enter image description here</a>
  My code:</p>
  
  <pre><code>&lt;?php
  
  class Display extends Awebarts
  {
      private $conn;
      private $tablename;
  
      public function __construct($tablename)
      {
  
          $this-&gt;tablename = $tablename;
  
          $this-&gt;connectToDb();
          $this-&gt;conn = $this-&gt;getConn();
          // insert the data into the table
          $this-&gt;getData();
  
          $this-&gt;close();
      }
  
      function getData()
      {
          $query = "SELECT * FROM $this-&gt;tablename ORDER BY `id` DESC LIMIT 1 ";
          if(!$sql = mysqli_query($this-&gt;conn, $query)) {
              throw new Exception("Error: Can not excute the query.");
          } else {
              $num = mysqli_num_rows($sql);
              while($num &gt; 0) {
                  //var_dump($data);
                  $data = mysqli_fetch_array($sql);
                  $num--;
              }
          }
          return $data;
      }
  }
  
  class Awebarts
  {
      private $cxn;
      private $conn;
  
      function connectToDb()
      {
          include "models/Database.php";
          $vars = "include/vars.php";
          $this-&gt;cxn = new Database($vars);
          $this-&gt;conn = $this-&gt;cxn-&gt;getConn();
      }
  
      function getConn()
      {
          return $this-&gt;conn;
      }
  
      function close()
      {
          $this-&gt;cxn-&gt;close();
      }
  }
  
  class Database
  {
      private $host;
      private $user;
      private $password;
      private $database;
      private $conn;
  
      function __construct($filename)
      {
          if(is_file($filename)) {
              include $filename;
          } else {
              throw new Exception("Error");
          }
          $this-&gt;host = $host;
          $this-&gt;user = $user;
          $this-&gt;password = $password;
          $this-&gt;database = $database;
          $this-&gt;connect();
          $this-&gt;selectData();
      }
  
      function getConn()
      {
          return $this-&gt;conn;
      }
  
      private function connect()
      {
          // connect to the sercer
          if(!mysqli_connect($this-&gt;host, $this-&gt;user, $this-&gt;password)) {
              throw new Exception("Error: not connected to the server");
          } else {
              $this-&gt;conn = mysqli_connect($this-&gt;host, $this-&gt;user, $this-&gt;password);
          }
          return $this-&gt;conn;
      }
  
      private function selectData()
      {
          if(!mysqli_select_db($this-&gt;conn, $this-&gt;database)) {
              throw new Exception("Error: No database");
          }
      }
  
      function close()
      {
          mysqli_close($this-&gt;conn);
      }
  }
  
  ?&gt;
  </code></pre>