doujiexi1824 2014-03-24 22:57
浏览 33
已采纳

如何将数组放入SQL?

I have the following php code getting a list of numbers from a Database:

  $current_product_id = "SELECT `product_id`,`category_id` FROM `oc_product_to_category` WHERE `product_id`='$product_id' ";   
  $current_product_ids = mysql_query($current_product_id); 

  $current_product_cat_ids='';

  while($current_product_cat_id = mysql_fetch_array($current_product_ids)){
$current_product_cat_ids.=$current_product_cat_id['category_id'].',';
}
  echo $current_product_cat_ids;

The code echos something like this: 84,109,140,146,151,152,

I would like to now take each of those numbers and somehow use them in another SQL query kinda like this:

$parent_category_ids = "SELECT `category_id`,`path_id` FROM `oc_category_path` WHERE `category_id`='84,109,140,146,151,152'"

So I can put the values into another SQL Query. Any ideas how I can do this? Thanks

  • 写回答

4条回答 默认 最新

  • duanqianhuan3994 2014-03-24 23:00
    关注

    Using the IN clause and the same variable:

    $res = mysql_query("SELECT `category_id`,`path_id` FROM `oc_category_path` WHERE `category_id` IN (" . rtrim($current_product_cat_ids, ',') . ")");
    while ($row = mysql_fetch_assoc($res)) {
        print_r($row);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号