I have the following classes:
class database {
protected $dbc;
protected $result;
public function __construct($host = 'localhost', $db_username = admin', $db_password = 'xxx', $db_name = 'yyy') {
$this->dbc = mysqli_connect($host, $db_username, $db_password, $db_name)
or die('Connection Error: ' . mysqli_connect_error($this->dbc)) ;
} //function
public function run_query($sql) {
$sql = addslashes($sql); //escape any problematic characters before executing query
$this->result = mysqli_query($this->dbc, $sql) or die (mysqli_error($this->dbc) . '<br /><br />' . $sql);
} //function
} //class
class user_access extends database {
public function __construct() {
session_name('YourSession');
session_start();
if (!isset($_SESSION['user_id'])) { //don't do anything for existing sessions
$this->run_query('INSERT visitor SET created = NOW()'); //log session
} //if
} //function
} //class
And I get the following error:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/.../classes/database.php on line 19
The __construct() in the user_access class isn't getting past the last line of code, but the error's occuring in the class it's extending.
When I try to run the same query in an instance of $database, rather than using $this-> inside the inheriting class, it works. I really don't think there is a problem with the SQL itself, nor the database query function I created, as it works with other queries or even this same query outside of the inheriting class.
I think it has to have something to do with inheritance, but I have no clue. Any help is much appreciated.
I've tried running the