Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No rows in result set when CodeIgniter's get() method has 0 as the second parameter [duplicate]

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');
    }           
}
like image 779
jesmarii Avatar asked Dec 03 '25 17:12

jesmarii


1 Answers

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

like image 146
Hasib Tarafder Avatar answered Dec 06 '25 08:12

Hasib Tarafder