kilrizzy kilrizzy - 1 month ago 5
MySQL Question

Codeigniter Active Record 'where_or' grouping

Is it possible to group and_where / or_where / or_like... statements together so as not to mix with other and/where statements.

Something that would result in


WHERE city = 'My City' AND state = 'My State' AND (name LIKE %name1%
OR name LIKE %name2%)

Answer

Yes. $this->db->where() allows you to write clauses manually. You could do this:

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->where('(name LIKE %name1% OR name LIKE %name2%)', null, false);

Setting the third parameter for false prevents CodeIgniter from trying to add backticks to the clause.

Check out the Active Record documentation. The section titled $this->db->where(); explains the four methods you can use to set WHERE clauses.