Hello i’m having problem in my php class, when i tried to return the data it outputs nothing.
Heres my code:
class Connection
{
public $connection;
function __construct($hostname,$username,$password,$database)
{
# code...
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
function setConnection(){
$this->connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
if(!$this->connection) die('connection problem');
return $this->connection;
}
}
//data class
class Data
{
public $datalist;
function __construct(Connection $connection)
{
# code...
$this->connection = $connection->setConnection();
}
function dataGrid()
{
$query = $this->connection->query('SELECT * FROM products');
if($query->num_rows > 0){
while(($this->datalist = $query->fetch())){
return $this->datalist;
}
}
}
}
i tried to call this in my index php using this but it outputs nothing:
$dbconnection = new Connection('localhost','root','','inventory');
$dbconnection->setConnection();
$dataresult = new Data($dbconnection);
$dataresult->dataGrid();
echo $dataresult->data_list['product'];
please anyone help me solve this, im trying to make my code maintainable so i use class, its my first time using oop so i don’t really get it much.