Michael Falciglia
Michael Falciglia

Reputation: 1046

Following a CodeIgniter Tutorial and recieving a Fatal Error

I am following a video tutorial for CodeIgniter, my code is exactly like the tutorial, but I am getting the following error:

Fatal error: Call to a member function get() on a non-object /public_html/application/models/site_model.php on line 7

Does anyone see what I am doing wrong with the code I have below?

This is line 7

$q = $this->db->get('test');

This is the full code of the page

class Site_model extends CI_Model {

  function getALL () {
    $q = $this->db->get('test');

    if($q->num_row() > 0) {
      foreach ($q->result() as $row) {
        $data[] = $row;
      }
      return $data; 
    }
  }
}

Upvotes: 0

Views: 87

Answers (3)

Mark
Mark

Reputation: 1386

Before you use the database object, you have to load it, by adding:

$this->load->database();

So after the changes it resembles this:

function getALL () {
    $this->load->database();
    $q = $this->db->get('test');

PS: I would recommend autoloading libs you will need throughout every page (a database object is usually one of them) or at least loading in the constructor.

Upvotes: 0

Suvash sarker
Suvash sarker

Reputation: 3170

Go to autoload.php in application/config/autoload.php and add this

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

Upvotes: 1

Jigar Chaudhary
Jigar Chaudhary

Reputation: 169

In CodeIgnitor you have use "$q->num_row()" to make that to "$q->num_rows()"

Change like below line

if($q->num_rows() > 0) {

Upvotes: 0

Related Questions