gpt4 book ai didi

php - fatal error : Uncaught Error: Call to a member function query() on null

转载 作者:行者123 更新时间:2023-11-29 02:14:25 25 4
gpt4 key购买 nike

<分区>

我是 OOP 的新手并且还在试验,有人可以指出我的错误在哪里吗?我遇到了错误

Fatal error: Uncaught Error: Call to a member function query() on null on the following line:

我意识到数据库连接不是从 Core 类继承的,但我不知道为什么。

$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']);

澄清:

我的 db 变量是从我在 index.php 页面开头包含的 config.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');

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com