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');

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

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包