这条线有什么问题吗?
我是 codeigniter 的新手,我一直试图找出这一行出了什么问题,
这是我的控制器
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);
}
}
,这是我的模型
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;
}
}
}
,这是我的错误,
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
错误就在这一行,
$query = $this->db->get('products');
为什么它失败了,codeigniter 文档是这样描述的...我还有一个产品表
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 error 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
在 application/config/autoload.php
try
in application/config/autoload.php
你需要检查两件事
1-您没有缺少构造函数
2-您已加载数据库。转到 application/config/autoload.php 并在自动加载库中添加“数据库”。
you need to check two things
1- you are not missing constructor
2- you have loaded database library. Go to application/config/autoload.php and add 'database' in autoload libraries.
您必须转到自动加载并将
$autoload['libraries']
替换为$autoload['libraries'] = array('database', 'form_validation');
you must go to autoload and replace
$autoload['libraries']
with this$autoload['libraries'] = array('database', 'form_validation');