gpt4 book ai didi

php - 消息 : Call to a member function get_users() on a non-object

转载 作者:可可西里 更新时间:2023-10-31 22:51:08 56 4
gpt4 key购买 nike

嗨,我正在使用 codeigniter,对此我还很陌生。我正在做一个简单的程序来显示数据库中的数据。但我有错误!这是我的代码

Controller 文件用户.php

class User extends CI_Controller{

public function show(){

$result= $this->user_model->get_users();

foreach($result as $object)
{
echo $object->id;
}
}



}

?>

模型文件用户模型.php

class User_model extends CI_Model {


public function get_users(){

$fetch= $this->db->get('User');
return $fetch->result();


}

}



?>

最佳答案

您需要将模型加载到您的 Controller 中。加载model 的最佳方式是为controller 编写constructor 并将所有需要的model 加载到其中。请在下面找到代码:

public function __construct()
{
parent::__construct();
$this->load->model('User_model');
}

只需将代码复制并粘贴到您的 Controller 文件中并检查。希望它能为您完成工作。

关于php - 消息 : Call to a member function get_users() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758822/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com