Call to a member function get() on null error

2020-04-10 03:28发布

I keep getting this error in CodeIgniter-2.2.1.(Fatal error: Call to a member function get() on null in C:\xampp\htdocs\ci\application\models\site_model.php on line 6) I am not sure why this is happening. Am I calling the function correctly? Line 6 is $this->load->model('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 The view page

<!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条回答
唯我独甜
2楼-- · 2020-04-10 03:53

Are you loading the database

$this->load->database();
查看更多
登录 后发表回答