person.php
<?php
class Person extends CI_Controller {
public function index()
{
echo 'Hello World! person';
$this->load->model('PersonModel');
$this->PersonModel->get_last_ten_entries();
}
}
?>
personModel.php
<?php
class PersonModel extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
function insert_entry()
{
$this->title = $_POST['title']; // please read the below note
$this->content = $_POST['content'];
$this->date = time();
$this->db->insert('entries', $this);
}
function update_entry()
{
$this->title = $_POST['title'];
$this->content = $_POST['content'];
$this->date = time();
$this->db->update('entries', $this, array('id' => $_POST['id']));
}
}
?>
收到此错误:
Fatal error: Call to a member function get() on a non-object in C:\wamp1\www\ci\CodeIgniter_2.1.3\application\models\personModel.php on line 18
请帮助解决此错误..
答案 0 :(得分:0)
CodeIgniter设置的常见错误是忘记加载数据库。本文档讨论了如何执行此操作:
http://ellislab.com/codeigniter/user-guide/database/connecting.html
最简单的方法是将“数据库”添加到 application / config / autoload.php
中找到的autoloading的库数组中示例:
$autoload['libraries'] = array('database', 'form_validation', 'session');