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
Call to a member function get() on null error
提问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();