开发者

what is wrong with this line?

I am new to codeigniter and i am stuck trying to figure out what is going wrong on this line

here is my controller

class Product extends CI_Controller{

    function index(){
        $this->load->model('product_model');
        $data['products'] = $this->product_model->get_all_products();
        $this->load->view('all_products', $data); 

    }
}

here is my model

class Product_model extends CI_Model {

    function get_all_products(){
        $query = $this->db->get('products');
        if($query->num_rows() > 0){
            foreach($query->result() as $row){
                $data[] = $row;
            } 
            return $data;
        }
    }
}

and here is my error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Product::$db

Filename: core/Model.php

Line Number: 50
Fatal error: Call to a member function get() on a non-object in /Users/matt/Sites/ci/application/models/product_model.php on line 9el.php on line 6

the er开发者_如何学JAVAror is on this line

$query = $this->db->get('products');

why is it failing the codeigniter documentation describes it that way...i have a products table also


try

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

in application/config/autoload.php


you need to check two things

1- you are not missing constructor

class Product_model extends CI_Model {

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

}

2- you have loaded database library. Go to application/config/autoload.php and add 'database' in autoload libraries.

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


you must go to autoload and replace $autoload['libraries'] with this $autoload['libraries'] = array('database', 'form_validation');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜