Message: calling get_users () member function for non-object

Hi, I am using codeigniter and I am very new to this. I am making a simple program that will display data from a database. but I have errors! this is my code

User.php Controller File   

class User extends CI_Controller{

    public function show(){

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

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



    }

?>

User_model.php Model File   

class User_model extends CI_Model {


    public function get_users(){

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


        }

    }



?>
+4
source share
1 answer

You need to load the model into your controller . The best way to load a model for writing a constructorfor the controller and load into it all the required model . Please find the following code:

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

. , .

+5

Source: https://habr.com/ru/post/1676248/


All Articles