Search code examples
phpsqlcodeignitersql-likequery-builder

Create multiple OR LIKE conditions on the same column using CodeIgniter query building methods


How can I perform a query with multiple LIKE parameters?

For example, I have this string to search through:

"I like searching very much"

This is the code I currently use:

$searTerm = "like"
$this->db->or_like('list.description', $SearchTerm,'both'); 

But I want to search with 2 or 3 parameters. like this:

$searTerm = "like";
$searTerm1 = "much";

How can I perform this to get the same result?


Solution

  • You can simply repeat the like parameters on the active record. In your example you would do something like this:

    $this->db->or_like('list.description', $searchTerm1);
    $this->db->or_like('list.description', $searchTerm2);
    $this->db->or_like('list.description', $searchTerm3);
    ...
    

    This will just join each or_like with an AND in the WHERE clause.