I know I have the error because of the scope in my method and the variable $conn is not defined there. How do I define that protected variable in my second class so I could make connection to my DB and fetch array.
[code] class Config{
private $host;
private $user;
private $pass;
private $db;
protected $conn;
// open connection to database
public function __construct() {
$this->db_connect();
}
static public function db_connect(){
$host = ”;
$user = ”;
$pass = ”;
$db = ”;
$conn = new mysqli($host, $user, $pass, $db);
if(mysqli_connect_errno()){
$msg = “Database connection failed: “;
$msg .= $conn->connect_error;
$msg .= ” (” . $conn->connect_errno . “)”;
exit($msg);
}
}
}
My second class
[code]class Database extends Config{
public function __construct(){
parent::__construct();
}
static public function find_by_sql($sql){
$result = $conn->query($sql);
if(!$result){
exit(“Database query failed.”);
}
//gather result into objects
$object_arr = [];
while($record = $result->fetch_assoc()){
$object_arr[] = htmlentities($record);
}
// result object method to free result set
$result->free();
}
}
now the use
[code]Database::db_connect();
$sql = “SELECT * FROM Events “;
$event = Database::find_by_sql($sql);
echo ‘<pre>’;
echo print_r($event);
echo ‘</pre>’;