PHP MVC database problem

Hello, so I am working on an MVC application. The problem that I’m having is with the database and connecting to it. So when I extends my controller, this is what it keeps saying.

Fatal error:  Call to a member function grab_records() on a non-object in F:\htdocs\model\records.php on line 43

Then if I change it from Controller to Model, this is what it says.

Notice:  Undefined property: Default_Records::$db in F:\htdocs\model\model.php on line 81

Fatal error:  Call to a member function prepare() on a non-object in F:\htdocs\model\model.php on line 81

For the first part, I believe it’s doing that because the model is where the $this->db is located, but then it should already be included in the controller because in the controller I have.

<?php
class Controller {

         public $db = null;
         public $model = null;

         function __construct() {
              $this->database();
              $this->model();
         }

         private function database() {
              $this->db = new mysqli('localhost', 'root', 'root', 'sample_database');
         }

         public function model() {
              require_once('model/model.php');
              $this->model = new Model($this->db);
         }

}

Here is my model.php file

<?php
class Model {

    public $model = null;

    function __construct($db) {

        try {
              $this->db = $db;
        } catch (mysqli_sql_exception $e) {
              exit('Database connection could not be established.');
        }

    }

    // $grab_title and $grab_artist are user inputs. Basically searching the database with the title and artist the user typed in.
    public function grab_records($grab_title, $grab_artist) {

              $stmt = $this->db->prepare("SELECT id, title, artist FROM records WHERE title = ? AND artist = ? LIMIT 1");
              $stmt->bind_param("ss", $grab_title, $grab_artist);
              $stmt->execute();
              $stmt->store_result();

              if($stmt->num_rows) {
                       $stmt->bind_result($record_id, $record_title, $record_artist, $record_date, $record_sold);

                        while($stmt->fetch()) {
                                return $record_id;
                                return $record_title;
                                return $record_artist;
                                return $record_date;
                                return $record_sold;
                         }

              } else {
                   $no_records = "Sorry, but there seems to be no records with that title and artist.";
                   return $no_records;
              }

    }

}

So I don’t know why using

class Default_Records extends Controller {

should be any problem. So I decided to extends the Model instead because I was thinking that maybe it’s because the controller requiring the Model class once. This time for the second part, I found out that it’s because anything that isn’t within the function __construct(), it won’t load up. I’ve tried actually include call my function via the construct operator and it works fine because the database is processing the query. However if I don’t use the construct to call for the function then I get either one of those errors.

Perhaps you need to be calling the parent::$this->db?
http://php.net/manual/en/keyword.parent.php

:slight_smile: Thank you so much. With your help, I got it to work. I had to call the parent constructor from the Controller.

Wow. Talked out my a** and it worked. Glad I could be of help :slight_smile: . Just beginning the OOP stuff myself.

3 Likes

:smiley: Very nice. Thanks a lot.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.