douxiang3978
douxiang3978
2015-03-02 19:30

子句mysql中逗号分隔的字符串IN数组

已采纳

I have a array like that :

 $myarray=  'zzz,aaa,bbb'  ; 

and want use it in mysql IN clause like that :

 "...  WHERE ids IN ($myarray) "   // didnt work 
 "...  WHERE ids IN ('$myarray') " // didnt work

the error im getting is that the first value in that array zzz says that zzz is not a column name . so i understand that i must separate the values with quotes to be like that :

 $myarray=  ' "zzz","aaa","bbb" '  ;

But i have no clue to do that . any help would be much appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doushan9415 doushan9415 6年前

    You need to explode() your string and then implode() it -

    $myarray = 'zzz,aaa,bbb'; 
    $realArray = explode(',', $myarray);
    $stringForIn = "'" . implode("','", $realArray) . "'";
    echo "WHERE ids IN ($stringForIn)";
    
    点赞 评论 复制链接分享
  • drc15469 drc15469 6年前

    Try that :

    $myarray=  'zzz,aaa,bbb'  ; 
    echo implode '"' . ('","', explode(',', $myarray)) . '"';
    
    点赞 评论 复制链接分享
  • doushan1850 doushan1850 6年前
    $myarray = 'zzz,aaa,bbb';
    $myarray = implode("','",explode(',',$myarray));
    $query   = "..... WHERE ids IN ('$myarray')";
    
    点赞 评论 复制链接分享