我已经改变了一些代码,但没有改变。我显然无法正确使用PDO。
所以,数据库连接在这里:
class Database {
private static $instance = null;
private function __construct(){}
public static function getInstance() {
if(is_null(self::$instance)){
self::$instance = new PDO("mysql:host = localhost, dbname = elektro", "root", "");
}
return self::$instance;
}
}
本课程涉及数据库:
abstract class ActiveRecord {
private static $conn;
public static function getAll(){
$conn = Database::getInstance();
$table = static::$table;
$class = get_called_class();
$q = self::$conn->query("select * from {$table}");
$res = $q->fetchAll();
return $res;
}
}
这个应该从一个表中获取所有行:
class Autor extends ActiveRecord {
public static $table = "autor";
}
$conn = Database::getInstance();
$allAutors = Autor::getAll();
print_r($allAutors);
这会导致致命错误:在null
上调用成员函数query()