Codeigniter like, or_like doesn't work with where Codeigniter like, or_like doesn't work with where codeigniter codeigniter

Codeigniter like, or_like doesn't work with where


Hello Can you please use following code

$q = $this->db->select('*')->from('table')->where('active', 1)->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)->get()->results;

Thanks


For an explanation of @Roshan's code because the answer may not be readily apparent to folks learning SQL in CI. (I also fixed errors in the code.)

$q = $this->db              // ->select('*') // you don't need select('*') if you want everything              // ->from('table') // Don't need this either              ->where('active', 1)              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)              ->get('table') // add your table name here              ->result();  // it's not "->results"

So your final query would look like this:

$q = $this->db->where('active', 1)              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)              ->get('table')              ->result(); 

Here's what the above is asking:

"get all results from 'table'     where active = 1 AND `col` = search_string1 OR where active = 1 AND `col` = search_string2 OR where active = 1 AND `col` = search_string3 OR where active = 1 AND `col` = search_string4 Then assign the result object to $q

A simpler way to view it but more complicated way to write the code (but maybe easier to understand):

$q = $this->db            // where active=1 AND col LIKE %$search_string1%             ->where('active', 1)             ->like('col', $search_string1, 'both')            // OR where active=1 AND col LIKE %$search_string2%             ->or_where('active', 1)             ->like('col', $search_string2, 'both')             // OR where active=1 AND col LIKE %$search_string3%             ->or_where('active', 1)             ->like('col', $search_string3, 'both')             // OR where active=1 AND col LIKE %$search_string4%             ->or_where('active', 1)             ->like('col', $search_string4, 'both')            ->get('table')            ->result();

There are times when you'll need to do it this way. For instance, if active may be several states. on, off, pending and you need to check that field for a specific state. Not necessary in your case as active is always 1, but if active could be more than one thing you want, you'll have to spell it out in your query builder.


You can use query grouping too.

Code will look like this:

$this->db->where('active', 1);$this->db->group_start();$this->db->or_like('col', $search_string1, both)$this->db->or_like('col', $search_string2, both)$this->db->or_like('col', $search_string3,  both)$this->db->or_like('col', $search_string4, both)$this->db->group_end();$q = $this->db->get('table')->result();

Using group_start and group_end assures that this part of or_like statements between then will get into brackets.