1 post
  • Has been part of the Envato Community for over 2 years

Hi all, Im trying to learn Object Oriented PHP but I’m having some difficulty. I have a database class which goes as follows:

       class database {
           public $dbHost = 'localhost';
           public $dbUser = 'root';
            public $dbPass = '';
           public $dbName = 'undergradpad';
            public $mysqli;

public function __construct(){

public function dbConnect(){
    ### not $mysqli
    $this->mysqli = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
    /* check connection */
    if (mysqli_connect_errno()){
        printf("Connect failed: %s\n", mysqli_connect_error());
        echo 'connection made';
    /* close connection */
    ### $this->db->close(); // DO NOT close the connection here!

       public function __destruct(){

This works fine the database connection is made, however I get this error when running a query:
 Fatal error: Call to a member function query() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/undergradpad2/house.class.php on line 19 
Here is the class I have made in relation:
class house {
        function __construct() {
            $mysqli = new database();

    * gethouses
    public function getHouses() {
     // Perform query
        $query = "SELECT * FROM houses";
        $result = $db->query($query);
        while ($row = $result->fetch_array(MYSQLI_ASSOC))
            $desciption = $row['description'];            
            echo "<p>$description</p>";    
This is also my index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

include ('house.class.php');
$house = new house();
echo $house->getHouses();

Would greatly appreciate any help. Thanks in advance!
32 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+2 more

In $result = $db->query($query);, to what is $db referring? I’d suggest adding a private variable $db to your house class, then setting $this->db = new database(); and $this->db->dbConnect();. You can then use $this->db->mysqli->query();.

If you want to go further, I would instead extend the MySQLi class with your database class.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes