duanci1858 2016-01-01 00:47
浏览 9
已采纳

如何根据不同的外部值在mysql表中显示行

I hope someone can help with the following... I have multiple product names in a MYSQL database table on one hand and on the other hand I am getting some info such as pricing (excluding product name and some other static info) from xml feeds. The xml feed gives a product code, with which I can match the rows I need in the database because the database includes name of the product but also product code whilst xml doesn't include name of the product.

What I want to do is to fetch the name of the product and some other values from the database and link them with the other info from the xml, so that the end result is that I display the name of the products and the other info on the front-end. There are multiple products from multiple xml's...

If this was single product with a single code, I know I can use the SELECT statement with a where clause, wherein the WHERE clause is the product ID, but because I have multiple products with multiple ID's and I don't need to fetch everything from the database (but only those products displayed/searched for on the front-end), I am a bit stuck and don't know how to do this.

I hope this makes sense. If not, please do let me know.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongyan1993 2016-01-01 00:58
    关注

    When you're processing the XML feed, put all the product codes into an array. Then you can construct the SQL query dynamically from this:

    $codes_string = implode(', ', $product_codes);
    $sql = "SELECT * FROM product_table WHERE product_id IN ($codes_string)";
    

    I'm assuming product IDs are numeric IDs. If they're strings you'll need to put quotes around each of them:

    $codes_string = implode("', '", $product_codes);
    $sql = "SELECT * FROM product_table WHERE product_id IN ('$codes_string')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看