4

HI im using codeigniter and i'm very new to this. i doing a simple program that will show data from database. but i'm having errors! this is my code

Controller file User.php

class User extends CI_Controller{

    public function show(){

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

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



    }

?>

Model file User_model.php

class User_model extends CI_Model {


    public function get_users(){

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


        }

    }



?>
dawood basharat
  • 165
  • 1
  • 5
  • 17
  • Which version of CI you use?? – Saty May 03 '17 at 11:31
  • @Saty sir this version CodeIgniter-3.1.4 – dawood basharat May 03 '17 at 11:32
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php); specifically [Fatal error: Call to a member function ... on a non-object](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983) – CBroe May 03 '17 at 11:33
  • You're calling a function on something that's not an object. I don't know CI but it looks like you haven't setup your model in your controller. – Jan Hančič May 03 '17 at 11:33
  • Possible duplicate of [CodeIgniter - Call to a member function select() on a non-object](http://stackoverflow.com/questions/8322124/codeigniter-call-to-a-member-function-select-on-a-non-object) – Masivuye Cokile May 03 '17 at 11:35
  • Possible duplicate of [Fatal error: Call to a member function where() on a non-object codeigniter $query->num_rows()==1)](http://stackoverflow.com/questions/12847242/fatal-error-call-to-a-member-function-where-on-a-non-object-codeigniter-quer) – Masivuye Cokile May 03 '17 at 11:36
  • Possible duplicate of [CodeIgniter - Call to a member function select() on a non-object](http://stackoverflow.com/questions/8322124/codeigniter-call-to-a-member-function-select-on-a-non-object) – Masivuye Cokile May 03 '17 at 11:37
  • Where you load model file?? – Saty May 03 '17 at 11:37
  • obviously $this->user_model seems not to be an object. you have to load it inside that class – Neo Morina May 03 '17 at 11:38
  • @JanHančič Sir my code is in front of you!! i'm trying to solve it but able to figure out the what is wrong!! that is why i asked for your help sir – dawood basharat May 03 '17 at 11:39
  • @NeoMorina yes sir i wasn't loading it!! thankyou very much sir – dawood basharat May 03 '17 at 11:42
  • 1
    @Saty i was missing that line!! thankyou fir helping sir!! really thankyou – dawood basharat May 03 '17 at 11:44

1 Answers1

5

You need to load the model into your controller. The best way to load the model to write a constructor for controller and load all required model into it. Please find the code below :

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

Just copy and paste the code into your controller file and check. Hope it will do the work for you.

Tristup
  • 3,603
  • 1
  • 14
  • 26