我是Codeigniter模型的新手,我尝试按照文档中的步骤加载所有在数据库中注册的用户。
这是我的模型: user.php
user.php
class User extends Model { function user() { parent::Model(); } function alluser() { $query = $this->db->query("select * from user limit 0,5"); //Line 30 error as in my IDE located in this line return $query->result(); } }
这是我的控制器: home.php
home.php
class home extends Controller { function index() { parent::Controller(); } function alluser() { $this->load->model('User'); $result = $this->User->showusers(); if ($result->num_rows()>0) { foreach ($result as $row) { echo "ID:".$row->userid." ".$row->userpenname."<br />"; echo $row->userfirstname." ".$row->userlastname."<br />"; } } } }
它显示错误:
A PHP Error was encountered Severity: Notice Message: Undefined property: User::$db Filename: models/user.php Line Number: 30
致命错误: 在第30行的G:\ xampp \ htdocs \ fiksi \ system \ application \ models \ user.php中的非对象上调用成员函数query()
第30行,请参阅上方的评论…
您必须先加载数据库库。在autoload.php添加:
autoload.php
$autoload['libraries'] = array('database');
另外,尝试将用户模型类重命名为“ User_model”。