0

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'];
        }
     }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo – Dharman Dec 09 '20 at 22:41
  • `$conn` is a local variable. You need to go back over the basics of OOP. – Dharman Dec 09 '20 at 22:43
  • @Dharman I know it is, I forgot to return it.. Problem SOLVED – benz-source Dec 09 '20 at 22:52
  • Sure, but please remove that `Dbh` class. You really do not need it. You should be passing mysqli instance around. Also, please enable error reporting for mysqli, it will save you a lot of problems – Dharman Dec 09 '20 at 22:53
  • 1
    Does this answer your question? [Fatal error: Call to a member function query() on null](https://stackoverflow.com/questions/30992830/fatal-error-call-to-a-member-function-query-on-null) –  Dec 09 '20 at 22:55
  • @CatchAsCatchCan nope it doesn't. It features a solution that's directly opposite to OOP which the OP is trying to learn. – Your Common Sense Dec 10 '20 at 07:36

0 Answers0