Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Call to a member function

i need help in debugging my code. Im new into php and im currently using codeigniter framework. Im trying to display the content of my database table to my page

/controllers/users.php

$<?php

class Users extends CI_Controller{

    function __Users(){

    // load controller parent
    parent::__Controller();

    // load 'Users' model
    $this->load->model('Users');
    }

    function index(){

    $data['users']=$this->Users->getUsersWhere('userid <',5);
    $data['numusers']=$this->Users->getNumUsers();
    $data['title']='Displaying user data';
    $data['header']='User List';

    // load 'users_view' view
    $this->load->view('users_view',$data);
    }
}
?>

/models/users.php

$<?php

class Users extends CI_Model{

function __Users(){

// call the Model constructor

parent::__CI_Model();

// load database class and connect to MySQL

$this->load->database();

}

function getAllUsers(){

$query=$this->db->get('admin_user');

if($query->num_rows()>0){

// return result set as an associative array

return $query->result_array();

}

}

function getUsersWhere($field,$param){

$this->db->where($field,$param);

$query=$this->db->get('admin_user');

// return result set as an associative array

return $query->result_array();

}

// get total number of users

function getNumUsers(){

return $this->db->count_all('admin_user');

}

}

?>

im having this error

Fatal error: Call to a member function getUsersWhere() on a non-object in C:\xampp\htdocs\printone\application\controllers\users.php on line 16

what could be the fault?

like image 332
nhoyti Avatar asked Oct 12 '22 02:10

nhoyti


1 Answers

You misnamed your controller constructor, so it isn't being called and your model isn't being loaded.

Change

function __Users(){

to

function __construct(){
like image 152
BoltClock Avatar answered Oct 27 '22 09:10

BoltClock