I have searched every single answer regarding this and none seem to fully answer what's going on. Currently I am getting the error in the title when trying to prepare a statement with pdo.
I have tried:
Using a constructor instead
Using non-static
Using $this when I had a constructor
Catching PDO exceptions, none seem to be thrown
Here is main.php
class db {
public static $db;
public static $db2;
public static function start() {
$host = "localhost";
$dbname = "dbname";
$usr = "user";
$ps = "pass";
$pdo = "mysql:host=$host;dbname=$dbname";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true
];
try {
db::$db = new PDO($pdo, $usr, $ps, $opt);
return db::$db;
} catch(PDOException $e) {
$e->getMessage();
}
}
}
And then to initialize the database, I call the start function in index.php with db::start();
Now, to the error.
Within main.php, in a whole different class and function, I try to call this connection statically.
$stmt0 = db::$db->prepare("SELECT * FROM users WHERE uname=:u");
But when done, I receive the error in the title, on this line.
Most would probably think, hmm, there must be an error with the connection then if it is calling it null. If that's the case, something is preventing my exception from being thrown because I see nothing in the error logs or on the page.