Search code examples
phpmysqlcodeigniterfind-in-set

Searching comma related value with FIND_IN_SET with multiple search string Codeigniter


I am using FIND_IN_SET to get similar comma related values from database the problem what i am facing is if in string i am passing single value it is searching accurately but if i am sending multiple value in string it is not able to search

$search  = "FIND_IN_SET('".$toteach."', level_whometoteach)";
$this->db->where($search);
        $query=$this->db->get();
        return $result = $query->result();

If here $toteach=5 and in level_whometotech 5 is present it search and give result but if,

$teach=5,6 and level_whometoteach contains 5,6 no value is returned 

Can i know the right way to do this


Solution

  • Well, FIND_IN_SET will check for individual values separated by comma. In your case, you should use IN clause.

    Try this query.

        $search  = "level_whometoteach IN (".$toteach.")";
        $this->db->where($search);
        $query=$this->db->get();
        return $result = $query->result();