Problem with PDO when executing php

l’m having a problem with PDO.
Everytime i run the php file. i get the following error :

Fatal error : Uncaught Error: Call to a member function prepare() on null…

The php file:

 try {
                //connection
                include '../connection.php';
                $database = new Database;
                $database->query('SELECT MAX(id) AS max_id FROM subscriber'); 
                $rows = $database->resultset();
                $id = $rows[0]["max_id"] + 1; // add with new ID
                ....

Connection.php

<?php

//defining the connection and other function that will be perform for linking with database
class Database
{   
    private $host = '';
	private $user = '';
	private $pass ='';
	private $dbname = 'data.db';
    private $dbh; //database handling
    private $error;
    private $stmt; //statements

    public function __construct()
    {   
        // Set DSN(Data Source Name)

        //SQLITE    
        $dsn = 'sqlite:'.$this->dbname;

        // Set Options
        $options = array(
            PDO::ATTR_PERSISTENT => true, 
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => true
        );

        // Create new PDO
        try { 
            $this->dbh = new PDO($dsn,$this->user,$this->pass,$options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null)
    {
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
            $this->stmt->bindValue($param, $value, $type);
        }
    }
//to execute prepared statements
    public function execute()
    {
        return $this->stmt->execute();
    }
//to select records from database
    public function resultset()
    {
        $this->execute();
        // retrieve the resultset in the form of an associative array
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }
//execute function lastInserted Id
    public function lastInsertId()
    {
        $this->dbh->lastInsertId();
    }
}

i try some solution on google but still having the error. Do i miss something ?

Thanks in advance,
Varoon

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.