I'm kind of new to OOP and very used to procedural programming. The only thing I try to do is to get the data from database using oop and spit it out into my HTML tables but I'm getting these messages:
Fatal error: Uncaught Error: Call to a member function query() on null in /var/www/public/kladd04/crud.php:9 Stack trace: #0 /var/www/public/kladd04/crud.php(27): User->getAllUsers() #1 /var/www/public/kladd04/index.php(35): ViewUser->showID() #2 {main} thrown in /var/www/public/kladd04/crud.php on line 9
CODE RIGHT HERE:
class Dbh {
private $servername;
private $username;
private $password;
private $dbname;
protected function connect(){
$this->servername = "localhost";
$this->username = "root";
$this->password = "root";
$this->dbname = "u04_todo";
$conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
}
}
class User extends Dbh {
protected function getAllUsers(){
$sql = "SELECT * FROM todos";
$result = $this->connect()->query($sql);
$num_rows = $result->num_rows;
if($num_rows > 0)
{
while($row = $result->assoc()) {
$data[] = $row;
}
return $data;
}
}
}
class ViewUser extends User {
public function showID(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['id'];
}
}
public function showTask(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['task'];
}
}
public function showImportance(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['importance'];
}
}
public function showStart(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['start'];
}
}
public function showDeadline(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['deadline'];
}
}
public function showStatus(){
$datas = $this->getAllUsers();
foreach($datas as $data){
echo $data['status'];
}
}
}