Hi all, Im trying to learn Object Oriented PHP but I’m having some difficulty when running the query I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/undergradpad2/house.class.php on line 19
I have a database class which goes as follows;
[php]<?php
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);
$this->mysqli->select_db(‘undergradpad’);
/* check connection /
if (mysqli_connect_errno()){
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}else{
echo ‘connection made’;
}
/ close connection */
### $this->db->close(); // DO NOT close the connection here!
}
public function __destruct(){
$this->mysqli->close();
}
}
?>
[/php]
Here is the class I have made to run the query
[php]<?php
class house {
function __construct() {
include(‘database.php’);
$mysqli = new database();
$mysqli->dbConnect();
}
/**
* 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>";
}
}
}
?>
[/php]
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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
[php]
<?php
include ('house.class.php');
$house = new house();
echo $house->getHouses();
?>
[/php]
</body>
</html>
Would greatly appreciate any help. Thanks in advance!