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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28230695/
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-25 23:45:51  来源:igfitidea点击:

Call to a member function get() on null error

phpcodeigniter

提问by Veronica

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');

我在 CodeIgniter-2.2.1 中不断收到此错误。(致命错误:在第 6 行 C:\xampp\htdocs\ci\application\models\site_model.php 中调用成员函数 get() on null)我不是确定为什么会发生这种情况。我是否正确调用了该函数?第 6 行是 $this->load->model('site_model');

controller site.php

控制器站点.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

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

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>

回答by vijaykumar

Are you loading the database

你在加载数据库吗

$this->load->database();