无法获取数据库

I am getting irritated by this error. I keep getting mysqli::query(): Couldn't fetch database on a mysqli query.

Here is my mysqli extended class (database.php) :

class database extends mysqli {

    private $host;
    private $username;
    private $password;
    private $database;
    private $name;
    private $connected;
    public $status;

    public function __construct( $param ) {

        $this->host = $param['host'];
        $this->username = $param['username'];
        $this->password = $param['password'];
        $this->database = $param['name'];   

        @parent::__construct( $this->host, $this->username, $this->password, $this->database );

        $this->status = ($this->connect_errno) ? "Not Connected ( Because of Error )" : "Connected";
        $this->connected = ($this->connect_errno) ? false : true;
    }

    public function stop() {
        if( $this->connected ) { $this->close(); }
        $this->status = "Not Connected ( Manually Disconnected )";
    }

}

And here is where i use the query (sessions.php) :

class session {

    private $db;

    public function __construct( $db ) {
        $this->db = $db;
    }

    function open() {
        return true;
    }

    function close() {
        return true;
    }

    function read( $id ) {


        $sql = "SELECT * FROM sessions WHERE sessionid = '{$id}'";
        $sqlq = $this->db->query( $sql );
        if( $sqlq ) { $row = $sqlq->fetch_assoc(); return $row['data']; }
        else { return ''; }

    }

    function write( $id, $data ) {

        $now = time();

        $sql = "SELECT * FROM sessions WHERE sessionid = '{$id}'";

        $sqlq = $this->db->query( $sql ); // HERE I GET THE ERROR

        if( ( $sqlq ) && ( $sqlq->num_rows == 1 ) ) { $sql = "UPDATE sessions SET data = '{$data}', updated = {$now} WHERE sessionid = '{$id}'"; return $this->db->query( $sql ); }
        else { $sql = "INSERT INTO sessions ( sessionid, updated, data ) VALUES ( '{$id}', {$now}, '{$data}' )"; return $this->db->query( $sql ); }

    }

    function destroy( $id ) {

        return $this->db->delete( "FROM sessions WHERE sessionid = '{$id}'" );

    }

    function clean( $max ) {

        $old = time() - $max;

        return $this->db->delete( "FROM sessions WHERE updated < {$old}" );

    }

}

This is how i use them in the main file (main.php):

<?
    $params = array();
    $params['host'] = XXXXXXXXXXXXX;
    $params['username'] = XXXXXXXXXXXXX;
    $params['password'] = XXXXXXXXXXXXX;
    $params['name'] = XXXXXXXXXXXXX;

    include("database.php");
    include("sessions.php");

    $db = new database( $params );
    $session = new session( $db );
    session_set_save_handler( array($session, "open"), array($session, "close"), array($session, "read"), array($session, "write"), array($session, "destroy"), array($session, "clean") );
    session_start();

?>

The read function should work ok, but the write function will not work with the same connection.

the best way is to create a connection inside your session object specific for the class itself.

It will be something like this:

class session {

  private $db;

  public function __construct( $params ) {
    $this->db = new database( $params );
  }
  .
  .
  .
}

and then create your session object like this:

$session = new session( $params );

Ok, I partially figured it out by myself. Anyway, it would be great if an experienced PHP developer would explain this more clearly for future issues and questions from other people.

It seems that if i check the status of my mysqli connection inside a session method, it shows up "Manually Disconnected", which means that $db->stop() have been called. I only call that function at the end of my index.php (the base index from my project) which basically means that everything that happens inside the session class happens AFTER the whole 'outside' PHP code have been run.

I have fixed my problem by opening another connection of my database class (The extension of mysqli), and using that connection inside session class.