phpcodeigniteractiverecordwhere-clausesql-like

CodeIgniter active record with where() and multiple or_like() logic


I'm writing a very simple search using like() and have an option to omit options, however I find the LIKE statement is making the query ignore the where statement.

$this->db->like('LOWER(location) OR LOWER(name)', strtolower($term));
$this->db->where('stage', 1);
$this->db->order_by("name", "asc"); 
$query = $this->db->get($this->user_table);
return $query->result();

Example of what the above produces with $term = "dublin";

SELECT * FROM (`users`) WHERE `stage` = 1 AND LOWER(location) OR LOWER(name) LIKE '%dublin%' ORDER BY `name` asc"

It still returns rows where 'stage' is not equal to 1.


Solution

  • $term = strtolower($term);
    $this->db->where("(LOWER(location) LIKE '%{$term}%' OR LOWER(name) LIKE '%{$term}%')");