Active entry codeigniter where, or_where?

I am using Active Record for CodeIgniter. I am confused by what approach I should take. Currently, our login system allows the user to use the username / email address to enter along with the password. But my current active record seems to allow the user to log in if he decides to use the email address + password.

Now this is my request:

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

if($query->num_rows>0)
  return TRUE;
else
  return FALSE;

Input Examples:

  • Username: test | Password: pass | Result: Success
  • Username: test | Password: blank | Result: Failed
  • : test@domain.com | : | :
  • : test@domain.com | : | :

Failed , , , .

+5
3

, AND OR WHERE. :

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

@RidIculous . :

$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');

, (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');
+4
$conditions = '(`username`="'.$username.'" OR `email`="'.$email.' OR `mobile`="'.$mobile.'"') AND `password`="'.$password.'"';          
$query = $this->db->get_where('table_name', $conditions);
$result = $query->result();
0

All Articles