I get the following error since I upgrade to PHP 7: "Fatal error: Uncaught Error: Call to a member function prepare() on null".
Here is my code:
class Database {
private $host = "localhost";
private $user = "xxx";
private $pass = "xxxx";
private $dbname = "xxxxx";
private $stmt;
private $dbh;
private $error;
public function __construct($dbname){
// Set DSN
$this->dbname = $dbname;
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="ALLOW_INVALID_DATES"'
);
// Create a new PDO instance
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
.......
}
How do I solve it? Thank you in advance.