Hi everyone. I'm trying to learn object oriented programming more clearer by creating a database class in PHP.
This is what i have right now. I'm getting an error about $mysqli being an undefined variable when i try to call it using $db->query();
Please explain how to make the variable $mysqli defined.
<?php
class phpDatabaseClass {
public function __construct()
{
$mysqli = new mysqli(DBhost, DBusername, DBpassword, DBname);
}
public function query()
{
$sql = 'select * from users';
$results = $mysqli->query($sql);
if(($results->num_rows) > 0)
{
echo 'We have: '.$results->num_rows;
}
}
}
?>
In another file i am instantiating the object and then calling a function like this:
require 'phpDatabaseClass.php';
define('DBhost', 'localhost');
define('DBusername', 'root');
define('DBpassword', 'root');
define('DBname', 'campbellCustomCoatings');
$db = new phpDatabaseClass();
$db->query();