Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function on a non-object in
#1

[eluser]mflammia[/eluser]
This is probably so simple but I can not see anything wrong with my syntax looking up how it should be done in the help.

I get the following error 'Fatal error: Call to a member function get_records() on a non-object in'

The actual mysql query returns a result if I run it straight in myPHPadmin

Model is called site_model.php

Code:
class Site_model extends CI_Model {
    
        function __construct()
        {
            // Call the Model constructor
            parent::__construct();
        }
        
        function get_records()
        {
            $query = $this->db->query('SELECT * FROM person');
            return $query->result();
        }
}

Controller is called site.php

Code:
class Site extends CI_Controller {

function databaselist()
{
            $data['query'] = $this->site_model->get_records();
            $this->load->view('databaselist',$data);
            
}
}

Many thanks.
#2

[eluser]LuckyFella73[/eluser]
Do you load your model somewhere?

Code:
function databaselist()
{
$this->load->model('site_model'); // missing ?

$data['query'] = $this->site_model->get_records();
$this->load->view('databaselist',$data);
}
#3

[eluser]mflammia[/eluser]
Fantastic! That sorted it, thank you very much.

The controller is fairly large and the model is getting loaded in another function - thought there must be a simple answer :-)
#4

[eluser]LuckyFella73[/eluser]
If you access the model in more than one
method of your controller you might load
the model in your constructor. Then you
have it available everywhere in that controller!




Theme © iAndrew 2016 - Forum software by © MyBB