codeigniter active record where, or_where?

前端 未结 3 978
小蘑菇
小蘑菇 2021-01-12 14:55

I am using Active Record on CodeIgniter. I am confused on which approach I should take. Currently, our login system let\'s the user to use username/email for the login along

相关标签:
3条回答
  • 2021-01-12 15:32

    @RidIculous is right. This is a correct way to do it:

    $user = $this->db->escape($user);
    $this->db->select('id,level,email,username');
    $this->db->where("(email = $user OR username = $user)");
    $this->db->where('password', $pass);
    $query = $this->db->get('users');
    

    Or a format I prefer (PHP 5+)

    $user = $this->db->escape($user);
    $query = $this->db
        ->select('id,level,email,username')
        ->where("(email = $user OR username = $user)")
        ->where('password', $pass)
        ->get('users');
    
    0 讨论(0)
  • 2021-01-12 15:35

    The issue is probably that you need to add brackets when mixing AND’s and OR’s in a WHERE clause. Try this:

    $this->db->select('id,level,email,username');
    $this->db->where("(email = '$user' OR username = '$user') 
                       AND password = '$pass'");
    $query = $this->db->get('users');
    
    0 讨论(0)
  • 2021-01-12 15:43
    $conditions = '(`username`="'.$username.'" OR `email`="'.$email.' OR `mobile`="'.$mobile.'"') AND `password`="'.$password.'"';          
    $query = $this->db->get_where('table_name', $conditions);
    $result = $query->result();
    
    0 讨论(0)
提交回复
热议问题