-3
<?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

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • This question appears to be off-topic because it is about an easy to google and fix Notice and a typo that has been asked and answered ad nauseam before and should not warrant yet another question about it. – Gordon Aug 04 '13 at 13:45
  • On a side note: why are you using PHP4 OOP? A lot of OOP is about encapsulation. Yet, your code uses no visibility modifiers whatsoever, effectively exposing all of your class internals. Check http://stackoverflow.com/questions/2206387 please. – Gordon Aug 04 '13 at 13:52

4 Answers4

1

There is nothing called $db - you want $this as its part of the class now so

 $getInfo = $this->db->query("SELECT * FROM threads");
exussum
  • 18,275
  • 8
  • 32
  • 65
1

The variable $db is not defined in your function. Change the line to:

$this->db->query
Schleis
  • 41,516
  • 7
  • 68
  • 87
0

Do not extend your classes from database class.

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.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

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");
svecon
  • 554
  • 1
  • 5
  • 8