douju2599 2018-02-28 08:56
浏览 34
已采纳

如何使用MYSQL IN Operator通过php数组获取多个记录

 $region_arr=array('Capital','ABC','Def');
 $RegionStr = implode(',', $region_arr);
 $sql="SELECT tax_id from bf_taxes where tax_region IN ('$RegionStr')";

This query is not working for me And when echoed it The result was

 SELECT tax_id from bf_taxes where tax_region IN ('Capital,ABC,DEF');

Which is wrong.The Query Should have been

 SELECT tax_id from bf_taxes where tax_region IN ('Capital','ABC','DEF');

Then the query would have given me the accurate result.So suggest me the changes i need to make in my code to achieve the desired result.

  • 写回答

1条回答 默认 最新

  • dongyuan9892 2018-02-28 09:14
    关注

    Try like this by preceding the quotes(') before and after the regions,

    $region_arr=array('Capital','ABC','Def');
    $RegionStr = "'".implode("','", $region_arr)."'"; //see this line
    $sql="SELECT tax_id from bf_taxes where tax_region IN ($RegionStr);";
    echo $sql;
    

    Output:

    SELECT tax_id from bf_taxes where tax_region IN ('Capital','ABC','Def');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失