1

I have a problem when fetching data from database. How can I fix this? My code:

class Disaster_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
        $this->table = 'disasters';
    }

    public function lists()
    {
        $data = $this->db->get($this->table);
        return json_encode($data->result());
    }
}

The errors are:

Undefined property: Disaster::$db

and

Call to a member function get() on null

2
  • Put out your $this->table from constructor set as private $table = 'disasters then access it via $data = $this->db->get($this->table); Commented Dec 31, 2014 at 15:25
  • Thanks, but I already figured out the problem, I just need to autoload database in database.php. Commented Dec 31, 2014 at 15:26

2 Answers 2

1

Oh, I forgot to autoload database on autoload.php.

I just added database in $autoload['libraries'] = array('database');

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

Comments

0

You need to setup the database connection in your code igniter config.

This is basically saying the ->db variable is null.

Setup your DB in Code Igniter

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.