我收到一个错误,说准备功能是空的,我之前已经有了这个并且可以解决它没有问题,但我不明白我现在做错了什么。致命错误显示:
致命错误:未捕获错误:调用成员函数prepare() null在C:\ xampp \ htdocs \ OOP \ Artist-System \ classes \ Db.php:43 Stack trace:#0 C:\ xampp \ htdocs \ OOP \ Artist-System \ classes \ Artist.php(94): Db->查询(' SELECT name,pl ...',Array)#1 C:\ xampp \ htdocs \ OOP \ Artist-System \ index.php(11):Artist-> select()#2 {main}抛出C:\ xampp \ htdocs \ OOP \ Artist-System \ classes \ Db.php on 第43行
Db类:
class Db
{
private $servername;
private $username;
private $password;
private $dbname;
private $charset;
public function __construct($servername, $username, $password, $dbname, $charset)
{
$this->servername = $servername;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->charset = $charset;
}
private function connect()
{
try {
$dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $this->username, $this->password, $options);
return $pdo;
} catch (PDOException $e) {
$e->getMessage();
}
}
public function query($query, $data)
{
$stmt = $this->connect()->prepare($query);
$stmt->execute($data);
$queryWords = explode(' ', trim($query));
if ($queryWords[0] == "SELECT") {
return $stmt->fetchAll();
}
}
艺术家类,错误位于第93-94行:
public function select()
{
$select = "SELECT name, playcount, listeners, image FROM artist";
return $this->db->query($select, array());
}