[英]Controller - Model in CodeIgniter and “Undefined property”
<?php
class Success_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//insert into user table
function get_all()
{
$query = $this->db->get('session'); // = select * from session
return $query->result();
}
}
<?php
class Success extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('success_model');
}
public function index()
{
$data= $this->Success_model->get_all();
$this->load->view('success_view', $data);
}
}
?>
As You can see this is good, but I have error: 如您所见,这很好,但是我有错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Success::$Success_model
Filename: controllers/success.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\session\application\controllers\success.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\session\index.php
Line: 292
Function: require_once
Fatal error: Call to a member function get_all() on null in C:\xampp\htdocs\session\application\controllers\success.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to a member function get_all() on null
Filename: controllers/success.php
Line Number: 15
Backtrace:
I'm looking, and looking, but I dont know what is bad. 我在看,在看,但是我不知道有什么不好。 The good directories are files, other classes are working but this not.
好的目录是文件,其他类正在工作,但是不行。
Please help. 请帮忙。 I'm just tired, because after 2 hours searching mistakes...
我很累,因为经过2个小时的搜索错误...
For CI v3, afaik 对于CI v3,afaik
I think I see it: 我想我看到了:
$data= $this->Success_model->get_all();
Should be: 应该:
$data = $this->success_model->get_all(); // Notice the lowercase success_model
One more solution would be to change 另一种解决方案是改变
$this->load->model('success_model');
into 成
$this->load->model('Success_model');
because then you can use 因为那样你就可以使用
$data= $this->Success_model->get_all();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.