I am an amateur, and therefore I have difficulty in solving the problem.
I have an abstract class, I will inherit 3 classes from it, for outputting and writing data to the database. Here is a code snippet:
class Database
public function __construct(){
$conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
}
Abstract class
abstract class Main_Product_Class
{
public function __construct(){
$this->db = new Database();
}
abstract public function getProducts($fields,$tablename);
}
class Product
class Product extends Main_Product_Class
{
public function __construct(){
parent::__construct();
}
public function getProducts($fields, $tablename){
$array = array();
$query = "SELECT $fields FROM $tablename ";
$result = mysqli_query($this->db, $query);
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
}
return $array;
}
}
index.php
$data = new Product();
$array = $data->getProducts('*', 'product');
I get an error
Warning: mysqli_query() expects parameter 1 to be mysqli, object given in Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in
Could you tell me how to solve the error and a few words about whether I am creating the structure correctly. I want to demonstrate code structuring in meaningful classes that extend each other, to see an abstract class for the main product logic.