phpcodeigniteractiverecordwhere-clauselogical-grouping

Mix of where() and or_where() not giving the intended result in CodeIgniter


I used where() and or_where() method calls in my model method, but the or_where method is not working as intended. What do I need to change here? If I hide the or_where() condition, it's working.

public function logincheck()
{
    $this->load->database();
    $mobile = $this->input->post('mobile');//rollno or mobile
    
    $status = '1';
    $user_type = 'student';
    $password = $this->input->post('password');
    
    $this->db->select('*')->from('student_creation')
        ->join('login', 'student_creation.student_id=login.login_id')
        ->where('login.user_type', $user_type)
        ->where('login.password', $password)
        ->where('student_creation.alternativemobile', $mobile)
        ->or_where('student_creation.rollno', $mobile);
    //$this->db->or_where('student_creation.alternativemobile', $mobile);
    
    $query = $this->db->get();
    //$rowcount = $query->num_rows();  
    if ($query->num_rows() == 1) {
        $row = $query->row();
        $data = array(
            'rollno' => $row->rollno,
            'student_id' => $row->student_id,
            'alternativemobile' => $row->alternativemobile,
            'login_id' => $row->login_id,
            'password' => $row->password,
            'user_type' => 'student',
            'loginchecks' => true
        );
        $this->session->set_userdata($data);
        return true;
    } else {
        return false;
    }
}

Solution

  • You cannot use multiple where and or_where like you have used.

    If your CI version is 3 or above, then you can use query-grouping function to achieve this. check this link https://codeigniter.com/user_guide/database/query_builder.html#query-grouping