codeigniter多个OR查询

时间:2012-11-26 08:45:34

标签: php codeigniter

我想在Codeigniter中使用以下查询:

SELECT `users` . * , `users_profile` . * FROM (`users`) LEFT JOIN `users_profile`
ON `users`.`id` = `users_profile`.`users_id`
WHERE (`loginid` = '$username' OR `email` = '$username' )
AND `password` = '$password' AND `users`.`status` = 'A' LIMIT 1 

它工作正常,但是当我以Codeigniter格式编写此查询时:

$this->db->where('loginid',"$username");
$this->db->or_where('email', "$username");    
$this->db->where('password',MD5($password));
$this->db->where('users.status','A');
$this->db->limit(1);

它将永远返回正确的语法?

2 个答案:

答案 0 :(得分:3)

可能是你的查询应该是这样的codeIgniter .......作为参考你可以看到这个链接 Reference

$this->db->select('*');
$this->db->from('users');
$this->db->join('users_profile', 'users_profile.id = users.id', 'left');
$this->db->where("(loginid = '$username' OR email = '$username') AND password = 'MD5($password) AND status = 'A')'");

答案 1 :(得分:1)

您应该使用where()的自定义字符串方法,因为这不能直接使用ActiveRecord。请参阅this link from the documentation并滚动到where()函数下的第4个选项。

示例:

    Custom string:

    You can write your own clauses manually:
    $where = "name='Joe' AND status='boss' OR status='active'";

    $this->db->where($where);

$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
相关问题