codeigniter active record where, or_where?

我的未来我决定 提交于 2019-12-01 04:49:34

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

@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');
$conditions = '(`username`="'.$username.'" OR `email`="'.$email.' OR `mobile`="'.$mobile.'"') AND `password`="'.$password.'"';          
$query = $this->db->get_where('table_name', $conditions);
$result = $query->result();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!