3

My model method always gives "No User Found". Why is it that my query never returns any rows?

When $page is 0 and $offset is 5, I expect to see the first 5 rows from the database table

Controller

public function filter($page = 0, $offset = 5, $search = '') { $search = $this->input->post('search'); $row = $this->m_user->getAllFilterUsers($offset, $page, $search); $data['usersTable'] = $row->result(); // more scripting } 

Model

public function getAllFilterUsers($offset, $count, $search) { if ($search != '') { $this->db->where('user_position', 'client'); $this->db->where('user_status', $search); } $this->db->where('user_position','client'); $this->db->where('user_status','active'); $this->db->order_by('user_id', 'desc'); $UsersQuery = $this->db->get('tb_user', $offset, $count); if($UsersQuery->num_rows>0){ return $UsersQuery; } else { $this->session->set_flashdata('message','No User Found'); redirect('cuser/filter','refresh'); } } 
2
  • 4
    doesnt work title for question. and no, we not debug your code. first find the cause of the error please. also are you doing error checking anywhere? Commented Aug 24, 2012 at 16:40
  • I recommend you check if your methods are receiving the necessary parameters. As @hakra said, we don't debug code, we need the error to find a solution. =) Commented Aug 24, 2012 at 16:45

1 Answer 1

1

Just check getAllFilterUsers function of your model. The value of $offset is 5 and value of $count is 0. But you use..

this->db->where('user_position','client'); $this->db->where('user_status','active'); $this->db->order_by('user_id', 'desc'); $UsersQuery = $this->db->get('tb_user',$offset,$count); 

that means..

Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 5,0 

This query always return empty set Your query should be..

Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 0,5 

So, Your code snippet should be

this->db->where('user_position','client'); $this->db->where('user_status','active'); $this->db->order_by('user_id', 'desc'); $UsersQuery = $this->db->get('tb_user',$count,$offset); 

Finally the function is...

public function getAllFilterUsers($offset,$count,$search){ if($search!=''){ $this->db->where('user_position','client'); $this->db->where('user_status',$search); } this->db->where('user_position','client'); $this->db->where('user_status','active'); $this->db->order_by('user_id', 'desc'); $UsersQuery = $this->db->get('tb_user',$count,$offset); if($UsersQuery->num_rows>0){ return $UsersQuery; } else{ $this->session->set_flashdata('message','No User Found'); redirect('cuser/filter','refresh'); } } 

Thats it

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.