What is wrong with this line?

I'm new to codeigniter, and I'm stuck trying to figure out what is going on in 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 mistake

 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 

error in this line

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

why this fails because the codeigniter documentation describes it this way ... I also have a product table

+4
source share
3 answers

to try

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

in application / config / autoload.php

+12
source

you need to check two things

1- you do not miss the constructor

 class Product_model extends CI_Model { function __construct(){ parent::__construct(); } } 

2- you downloaded the database library. Go to application / config / autoload.php and add the "database" to the startup libraries.

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

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

0
source

Source: https://habr.com/ru/post/1347307/


All Articles