CodeIgniter "WHERE AND OR" 到 Active Record

CodeIngiter "WHERE AND OR" to Active Record

我想像这样进行 CodeIgniter Active Records 查询。

SELECT * FROM user WHERE user_name = 'email' 
AND (user_password='123' OR second_password = '456')

我已经尝试了一些代码,但没有用。我正在使用 CI 2.2.0

根据 CI 文档,您可以为 where 子句编写自定义字符串 https://ellislab.com/codeigniter/user-guide/database/active_record.html

$this->db->from('user');
$where = "user_name='email' AND (user_password='123' OR second_password = '456')";
$this->db->where($where);
$query = $this->db->get();

Shorthand版本:

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where('username', $name)->where($where)->get('myTableName');

$this->db->where() 已经充当 SELECT FROM * 除了你只想 select 某些列。

$this->db->select(); Note: If you are selecting all (*) from a table you do not need to use this function

我有一个简单的解决方案,谢谢大家。

$this->db->select('*');
$this->db->from('user');
$this->db->where('user_name', 'email');
$this->db->where('user_password', '123');
$this->db->or_where('user_name', 'email');
$this->db->where('second_password', '456');
$query = $this->db->get();