codeigniter活动记录,其中,or_where? [英] codeigniter active record where, or_where?
问题描述
我使用的codeIgniter活动记录。我很困惑哪个方法,我应该采取。目前,我们的登录系统,让用户在使用的用户名/邮箱的登录及密码。但我的当前活动的记录,似乎让用户登录,如果他选择使用电子邮件+没有密码。
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 with the password. But my current active record, seems to let the user logged in if he choose to use the email + no password.
现在,这是我的查询:
$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;
采样输入:
- 用户名:测试 |密码:通过 |结果: 成功的
- 用户名:测试 |密码:空的|结果: 失败的
- 用户名: test@domain.com |密码:通过 |结果: 成功的
- 用户名: test@domain.com |密码:空的|结果: 成功的
- Username: test | Password: pass | Result: Success
- Username: test | Password: empty | Result: Failed
- Username: test@domain.com | Password: pass | Result: Success
- Username: test@domain.com | Password: empty | Result: Success
第四个测试输入必须的的无法的的结果,但似乎它会记录即使密码为空的用户。
The fourth test input must be Failed in result, but it seems that it logs the user even if the password is empty.
推荐答案
这个问题可能是,你混合和的时候和OR在WHERE子句需要加括号。试试这个:
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');
这篇关于codeigniter活动记录,其中,or_where?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!