致命错误:调用第 6 行 C:\wamp\www\ci\application\models\site_model.php 中非对象的成员函数 get()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9629321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:18:39  来源:igfitidea点击:

Fatal error: Call to a member function get() on a non-object in C:\wamp\www\ci\application\models\site_model.php on line 6

phpcodeigniter

提问by Matt

Hello I just watched the first/Day1 screencast on Nettuts "CodeIgniter from scracth" And I'm already running into an error I don't understand. Here's a screenshot http://i39.tinypic.com/14mtc0n.jpg

您好,我刚刚在 Nettuts“CodeIgniter from scracth”上观看了第一个/Day1 截屏视频,我已经遇到了一个我不明白的错误。这是截图http://i39.tinypic.com/14mtc0n.jpg

The code in my models\site_model.php is the same as the screencast

我的模型\site_model.php 中的代码与截屏相同

   models\site_model.php

   class Site_model extends CI_Model {  
   function getAll() {
    $q = $this->db->get('test');        
    if($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
            $data[] = $row;
        }
    return $data;
    }
}

And the controller controllers\site.php

和控制器控制器\site.php

   class Site extends CI_Controller {
function index(){
    $this-> load-> model('site_model'); 
    $data['records'] = $this-> site_model-> getAll();
    $this-> load-> view('home', $data);

}   
 }

And here's my db info incase

这是我的数据库信息 incase

 $db['default']['hostname'] = 'localhost';
 $db['default']['username'] = 'root';
 $db['default']['password'] = '';
 $db['default']['database'] = 'ci_series';
(rest is default below)

Thank you

谢谢

回答by JohnP

You need to load the database first. Codeiginiter won't load it by default for you.

您需要先加载数据库。默认情况下,Codeiginiter 不会为您加载它。

You can either add it to /config/autoload.phplike so

你可以添加它/config/autoload.php喜欢这样

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

Or you can load it on demand whenever you want by calling

或者您可以随时通过调用按需加载它

$this->load->database();

More details here

更多细节在这里

http://codeigniter.com/user_guide/database/connecting.html

http://codeigniter.com/user_guide/database/connecting.html

回答by Sudhir Bastakoti

Add constructor to you model, if you havenot:

将构造函数添加到您的模型中,如果您还没有:


class Site_model extends CI_Model {  

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

And: autoload database by changing application/config/autoload.php if you have not autoloaded

并且:如果您还没有自动加载,则通过更改 application/config/autoload.php 自动加载数据库