Mysql search with comma delimited string

前端 未结 3 614
花落未央
花落未央 2020-12-21 19:56

I have 1 mysql table called colors with rows id and name

1 - yellow
2 - black
3 - red
4 - green
5 - white
6 - bl         


        
3条回答
  •  伪装坚强ぢ
    2020-12-21 20:27

    $array = explode(",", $colors);
    $search = implode("', '", $array); // implode instead of impode
    
    $sql = "
    SELECT
      id,
      name
    FROM
      colors
    WHERE
      name IN ('$search')
    ";
    
    $result = mysql_query($sql);
    
    while ($row = mysql_fetch_array($result)) {
        //do something with the matches
    }
    

提交回复
热议问题