0

I was looking around for this problem a lot! But nothing realy helped to solve this issue.

I always get the following error: "Fatal error: Call to a member function query() on a non-object"

I have the following Controller:

<?php
class Station extends CI_Controller {

    function __construct($params=array()) {
        parent::__construct();

        session_start();

        $this->load->library('parser');
        $this->load->helper(array('form', 'url'));
    }

    public function index() {
        $this->tpl_data['location'] = $this->stationmodel->getLocations()->all;
        $_SESSION['location'] = $this->tpl_data['location'];


        $this->load->view('station', $this->tpl_data);
    }
}

?>

And the following Model:

<?php
class StationModel extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getLocations() {
        $query = $this->db->query("SELECT * FROM location WHERE City_IATA = 'MUC'");
        var_dump($query);

        // Just for test purposes!
        foreach($query->result() as $row) {
            echo $row->Location_Name_DE . '<br />';
        }
    }
}

?>

In my autoload.php I load the database module:

$autoload['libraries'] = array('database');

Also when I try to load the database modul directly in the controller I will get the same error.

My database.php:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'comvel';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Anyone can help me out?

Thanks in advance!

11
  • And what returns the var_dump? Commented Apr 24, 2012 at 10:42
  • did you check your database settings? Commented Apr 24, 2012 at 10:43
  • The var_dump returns nothing, cause it dies before the var_dump. The database settings are checked and fine :-/ Commented Apr 24, 2012 at 11:09
  • How many results do you want query to fetch single or multiple if single then use $query->row() Commented Apr 24, 2012 at 11:33
  • I want multiple results. But my problem is, that the line $query = $this->db->query("SELECT * FROM location WHERE City_IATA = 'MUC'"); creates the error "Fatal error: Call to a member function query() on a non-object" Commented Apr 24, 2012 at 11:54

2 Answers 2

1

In your controller try removing $params=array() from function __construct($params=array())

I would also remove the __construct() {} from your model as it doesn't do anything and might cause you trouble.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the hint! I Changed the __construct in the controller and removed the __construct from the model. But nothing changed. Still receiving the error.
Try var dumping $this->db before the $this->db->query() line.
The var_dump($this->db) return null. So the database isn't initialized. But the database module is set in the config/autload.php and the parameters in the config/database.php are also fine. Do I have to set anything else?
0

This is a weird one. Seems like you've done everything right.

Have you tried removing the constructors altogether? My guess is that your constructor is somehow preventing your model from being initialized with the db object. If you need to run some initialization code, it may be more appropriate to use a hook, such as the pre_controller hook.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.