一個成員函數查詢()我是一個新手,OOP和尚在測試階段,能有人指出哪裏我的錯誤是什麼? 我收到提示致命錯誤:未捕獲的錯誤:調用上的空
Fatal error: Uncaught Error: Call to a member function query() on null on the following line:
我已經意識到,DB連接沒有被從核心類繼承,但我不知道爲什麼。
$user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");
class.php
class Core {
public $db;
public $domain;
public $settings;
public function getDomain() {
return $this->domain;
}
function __construct($db,$domain) {
$this->db = $db;
$this->domain = $domain;
$this->getSettings();
}
public function getSettings() {
$settings = $this->db->query("SELECT * FROM settings");
$settings = $settings->fetch_object();
$this->settings = $settings;
}
}
class User extends Core {
public $user;
function __construct($user_id) {
$this->setUser($user_id);
}
public function setUser($user_id) {
$user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1");
if($user->num_rows >= 1) {
$user = $user->fetch_object();
$this->user = $user;
}
}
的index.php
$core = new Core($db,$domain);
$user = new User($_SESSION['user_id']);
澄清:
我的數據庫變量從我的config.php文件通過我包括index.php頁面的開始 的config.php
$_db['host'] = 'localhost';
$_db['user'] = 'root';
$_db['pass'] = 'root';
$_db['name'] = 'social';
$db = new mysqli($_db['host'], $_db['user'], $_db['pass'], $_db['name']) or die('MySQL Error');
什麼時候開始的會議?該會話數組是否包含一個值? –
是的,會議已經開始。我認爲這是事做不被繼承了$ DB變量或東西 –
一個正在進行幾件事情。您可以在寫入$ core = new Core($ db,$ domain)之前展示如何設置變量$ db? – Yolo