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

子句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.

图片转代码服务由CSDN问答提供 功能建议

我有一个这样的数组:

  $ myarray =  'zzz,aaa,bbb';  
   
 
 

并希望在mysql IN子句中使用它:

 “... WHERE ID IN(  $ myarray)“//没有工作
”... WHERE ids IN('$ myarray')“//没有工作
   
 
 

我得到的错误是 该数组 zzz 中的第一个值表示zzz不是列名。 所以我明白我必须用引号将值分开:

  $ myarray ='“zzz”,“aaa”,“bbb”'; 
 <  / code>  
 
 

但我不知道这样做。 任何帮助将不胜感激。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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)";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushan1850 2015-03-02 19:34
    $myarray = 'zzz,aaa,bbb';
    $myarray = implode("','",explode(',',$myarray));
    $query   = "..... WHERE ids IN ('$myarray')";
    
    评论
    解决 无用
    打赏 举报
  • drc15469 2015-03-02 19:35

    Try that :

    $myarray=  'zzz,aaa,bbb'  ; 
    echo implode '"' . ('","', explode(',', $myarray)) . '"';
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题