在null错误上调用成员函数get()

时间:2015-01-30 06:53:47

标签: php codeigniter

我一直在CodeIgniter-2.2.1中收到此错误。(致命错误:在第6行的C:\ xampp \ htdocs \ ci \ application \ models \ site_model.php中调用null上的成员函数get())我不确定为什么会这样。我正确地调用了这个函数吗?第6行是$ this-> load->模型(' site_model');

controller site.php

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

site_model.php

<?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;
   }
 }
}
?>

home.php查看页面

<!DOCTYPE>

<html>
<head>
    <title>Site</title>
</head>
<body>
    <div id="container">
        <p>My view has been loaded</p>
    <pre>

<?php print_r($records);?>
    </pre>

    </body>
    </html>

1 个答案:

答案 0 :(得分:7)

您是否正在加载数据库

$this->load->database();