1

人.php

<?php
class Person extends CI_Controller {

    public function index()
    {
        echo 'Hello World! person';

        $this->load->model('PersonModel');

        $this->PersonModel->get_last_ten_entries();
    }
}
?>

人模型.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

我该如何解决这个错误?

4

1 回答 1

0

CodeIgniter 设置的一个常见错误是忘记加载数据库。本文档讨论了如何做到这一点:

http://ellislab.com/codeigniter/user-guide/database/connecting.html

最简单的方法是将“数据库”添加到库数组中,以便在application/config/autoload.php中找到自动加载

例子:

$autoload['libraries'] = array('database', 'form_validation', 'session');
于 2013-04-15T17:21:32.580 回答