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

Mat*_*att 3 php codeigniter

你好,我刚看了"从scracth笨"上NETTUTS第一/第一天的截屏而我已经运行到一个错误,我不明白.这是一张截图http://i39.tinypic.com/14mtc0n.jpg

我的models\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;
    }
}
Run Code Online (Sandbox Code Playgroud)

和控制器controller\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);

}   
 }
Run Code Online (Sandbox Code Playgroud)

这是我的数据库信息

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

谢谢

Joh*_*hnP 11

您需要先加载数据库.Codeiginiter默认不会为您加载它.

您可以将其添加到/config/autoload.php这样

$autoload['libraries'] = array('database');
Run Code Online (Sandbox Code Playgroud)

或者您可以随时通过呼叫加载它

$this->load->database();
Run Code Online (Sandbox Code Playgroud)

更多细节在这里

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