这条线有什么问题吗?

发布于 2024-10-31 06:38:51 字数 1144 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-11-07 06:38:51

尝试

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

在 application/config/autoload.php

try

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

in application/config/autoload.php

情绪少女 2024-11-07 06:38:51

你需要检查两件事

1-您没有缺少构造函数

class Product_model extends CI_Model {

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

}

2-您已加载数据库。转到 application/config/autoload.php 并在自动加载库中添加“数据库”。

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

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');
不及他 2024-11-07 06:38:51

您必须转到自动加载并将 $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');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文