douxiang3978 2015-03-02 19:30
浏览 672
已采纳

子句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 2015-03-02 19:35
    关注

    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)";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?