Codeigniter Search Comma-separated values in MySql

Codeigniter Search Comma-separated values in MySql-There are multiple ways to search Comma-separated values in MySql. We sometimes need to filter some data using comma separated values. Here in this article we are going to explain how you can add filter on column using Active records in Codeigniter for comma-separated values.

Codeigniter Search Comma-separated values in MySql | Find In Set

We can use find in set(find_in_set) in Active records to search comma separated values. Here is simple Example to search value 1,2,5,6 in mysql codeignater

    $searchValues  = [1,2,5,6];
    $this->db->select('*')->from('users');
    $this->db->group_start();
    foreach($searchValues as $value)
    {
        $this->db->where("find_in_set($value, user_id)");
    }
    $this->db->group_end();
    $result = $this->db->get()->result_array();

The above example will give you list of users where user id is – 1,2,5 and 6.


Advertisements

Add Comment