doubei3312 2013-06-12 11:45
浏览 7
已采纳

代码点火器错误? [关闭]

Can somebody help me, I am new in CI, and I don't know what do I do wrong.

I have this kind of controller

main

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class main extends CI_Controller {
 public function index() {
     $this->home();
 }

 public function home() {
    $this->load->helper('url');
    $this->load->model("get_db");
    $data['results']=$this->get_db->getAll();
    $this->load->view("view_home", $data);
 }
}

And the model get_db like this

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class get_db extends CI_Model {
        public function getAll() {
        $query=$this->db->query("SELECT * from tekstovi");
        return $query->result();
    }
}

I got this strange error like this

**A PHP Error was encountered
Severity: Notice
Message: Undefined property: main::$db
Filename: core/Model.php
Line Number: 51**

Any idea what to correct?

  • 写回答

3条回答 默认 最新

  • dp6319 2013-06-12 12:50
    关注

    You need to add __construct() to your controller and model. Try the following.

    Your controller

    class main extends CI_Controller 
    {
        function __construct()
        {
            parent::__construct();
        }
    
    public function index() 
    {
         $this->home();
    }
    
    public function home() 
    {
        $this->load->helper('url');
        $this->load->model("get_db");
        $data['results']=$this->get_db->getAll();
        $this->load->view("view_home", $data);
       }
    
     }
    

    Your model.

    class Get_db extends CI_Model
    {
        function __construct()
        {
        parent::__construct();
    
        }
    
        // here is your code
        public function getAll() {
            $query=$this->db->query("SELECT * from tekstovi");
            return $query->result();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算