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?
You misnamed your controller constructor, so it isn't being called and your model isn't being loaded.
Change
function __Users(){
to
function __construct(){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With