title: PHP closing mysqli connection to database error
body: <p>The case in which I'm getting this error is diffrent from what I have seen on SO, in trying to solve my problem. I have a class, Database, which creates only one instance of itself, trying to limit the number of connection to mysql server to one. Here is my class <strong>[code 1]</strong>: </p>
  
  <pre><code>class Database {
  
      private $_connection;
  
      // Store the single instance.
      private static $_instance;
  
      /**
       * Get self instance of database to private static variable $_instance.
       * @param string $host
       * @param string $username
       * @param string $password
       * @param string $database
       * @return Database
       */
        public static function getInstance($host,$username,$password,$database) {
            if (!self::$_instance) {
                self::$_instance = new self($host,$username,$password,$database);
            }
            return self::$_instance;
        }
  
        /**
         * Constructor.
         * @param string $host
         * @param string $username
         * @param string $password
         * @param string $database
         */
         public function __construct($host,$username,$password,$database) {
             $this-&gt;_connection = new mysqli($host, $username, $password, $database);
  
          // Error handling.
          if (mysqli_connect_error()) {
              trigger_error('Failed to connect to MySQL: ' . mysqli_connect_error(), E_USER_ERROR);
          }
      }
  
      /**
      * Empty clone magic method to prevent duplication.
      */
       private function __clone() {}
  
       /**
       * Get the mysqli connection;
       */ 
        public function getConnection(){
            return $this-&gt;_connection;
        }
    }
  </code></pre>
  
  <p>After this class, I created a connection for getting some info from a table. The code is the following <strong>[code 2]</strong>: </p>
  
  <pre><code>    // Establish a connection with MySQL Server database.
      $host = 'localhost';
      $username = 'barbu';
      $password = 'watcrn0y';
      $database = 'siteinfo';
  
      $db = Database::getInstance($host, $username, $password, $database);
      $mysqli = $db-&gt;getConnection();
  
      // Get the firstname of the author of the site from database.
      $sql_query = 'SELECT author.firstname, author.lastname ';
      $sql_query .= 'FROM author;';
  
      $result = $mysqli-&gt;query($sql_query);
  
  
      if($result &amp;&amp; $row = $result-&gt;fetch_assoc()){
          $author_firstname = $row['firstname'];
          $author_lastname = $row['lastname'];
      }
  </code></pre>
  
  <p>Now, in another file, I do this <strong>[code 3]</strong>: </p>
  
  <pre><code>require '../includes/siteinfo.php'; // this file contains the connection 
      // with first database ['siteinfo']. 
      //I include this file for accessing some other variables from it, which aren't  
     //in the code posted above. 
  
  // Establish the connection to server.
  $host = 'localhost';
  $username = 'barbu';
  $password = 'watcrn0y';
  $database = 'articles';
  
  // Here I need to close my previous connection and begin another.
  // It is important to remember that my class is designed for creating 
  // just one connection at a time.
  // So if I want to change the connection to another database, 
  // I have to close the previous one and create the one which 
  // suits my needs.
  $mysqli-&gt;close();
  unset($db);
  
  $db = Database::getInstance($host, $username, $password, $database);
  $mysqli = $db-&gt;getConnection();
  
  
  
  // Send the post info to the server.
  $title = (isset($_POST['title'])) ? $_POST['title'] : '';
  $text = (isset($_POST['text'])) ? $_POST['text'] : '';
  $sql = "INSERT INTO postInfo ";
  $sql .= "VALUES ('" . $author_firstname . " " . $author_lastname ."', '" . 
  date('d D M Y') . "', '" . $title . "', '" . $text . "');";
  $result = $mysqli-&gt;query($sql);
  </code></pre>
  
  <p>While doing this, I get the error: </p>
  
  <blockquote>
    <p>Warning: mysqli::query(): Couldn't fetch mysqli in /home/barbu/blog/admin/index.php on line 24</p>
  </blockquote>
  
  <p>If I don't close the first connection (and let say I let just ['unset($db)']), my query will be executed on the first database ['siteinfo'], and I'm getting another error message, namely one telling me that the 'postInfo' table doesn't exist in 'siteinfo' database, which is true. If I let that connection to persist, and declare another instance of Database class, $db1, and another mysqli object, $mysqli1, which hold my connection, and execute my query through it, I get the same mysqli error message as in second case: 'siteinfo.postInfo' doesn't exist. What do you recommand me? How can I solve this problem?</p>