我试图创建一个有点简单的数据库抽象层。一切似乎进展顺利,直到我尝试用一个无法使用的查询来测试它。寻求这方面的一点帮助。
错误:
( ! ) Notice: Undefined variable: dbh in C:\wamp64\www\phpwebsite\db.php on line 29
( ! ) Fatal error: Call to a member function query() on null in C:\wamp64\www\phpwebsite\db.php on line 29
数据库类:
include_once 'IAbstractDatabase.php';
class db implements IAbstractDatabase
{
public function __construct()
{
$username = "root";
$pass = "";
try {
$dbh = new PDO("mysql:host=localhost;dbname=imperialcars", $username, $pass);
} catch (PDOException $e) {
echo $e->getMessage()."</br>";
die();
}
}
public function __destruct()
{
$dbh=null;
}
public function select($query)
{
return $stmt = $dbh->query($query);
}
public function query($query)
{
return $stmt = $dbh->query($query);
}
}
$dbh = new db();
$dbh->query("INSERT INTO users (userid, username, userfname, userlname, email, password, banned, admin) VALUES (NULL, 'user2', 'john', 'doe', 'johndoe@hotmail.com', 'password', '0', '0')");
感谢您的帮助和反馈。