<?php
// Forum Configuration
define('DB_HOST','localhost'); // Database Hostname
define('DB_USER','root'); // Database Username
define('DB_PASSWORD',''); // Database Password
define('DB_NAME','ultraforum'); // Database Name
define('WEB_NAME','Website Name'); // Website Name
define('WEB_TITLE','Website Title'); // Website Title
// Do not modify anything under this line :)
class db {
var $dbhost;
var $dbuser;
var $dbpassword;
var $dbname;
var $query;
function connect() {
$this->db =
new mysqli($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname);
}
function __construct() {
$this->dbhost = DB_HOST;
$this->dbuser = DB_USER;
$this->dbpassword = DB_PASSWORD;
$this->dbname = DB_NAME;
}
}
And my Forum class extending DB
class forum extends db{
public function __construct() {
parent::__construct();
}
function displayInfo () {
$this->forumInfo =
$getInfo = $db->db->query("SELECT * FROM threads");
while($getI = $getInfo->fetch_object()) {
return $getI->Title;
}
}
}
With the second class, I want to get all of the threads from my mysqli database. I extended it from the first class because I wanted the connection information. This is how im implementing the class:
$ThreadInfo = new forum;
$ThreadInfo->displayInfo();
But I get
Notice: Undefined property: forum::$db on line 42
Fatal error: Call to a member function query() on a non-object in on line 42
Firstly, your connection is not instantiated, you need to call connect() in your db class.
function __construct() {
$this->dbhost = DB_HOST;
$this->dbuser = DB_USER;
$this->dbpassword = DB_PASSWORD;
$this->dbname = DB_NAME;
$this->connect();
}
Secondly you want to access $db in class' scope:
$getInfo = $this->db->query("SELECT * FROM threads");
There is nothing called $db - you want $this as its part of the class now so
$getInfo = $this->db->query("SELECT * FROM threads");
The variable $db
is not defined in your function. Change the line to:
$this->db->query
This is what you are doing wrong. In may ways.
Pass an instance of your $db class into forum:
class forum {
private $db;
public function __construct($db) {
$this->db = $db;
}
function getInfo () {
}
}
Also, code of your displayInfo() function is all wrong.
you need to learn basic mysqli operations before starting for other classes.