簡體   English   中英

PHP Mysqli 幫助 從多個表中選擇

[英]PHP Mysqli Help Selecting from multiple tables

所以..我有兩個像這樣的mysqli表:

第一個表名:訂購項目

在此處輸入圖片說明

第二個:表名:卡片

在此處輸入圖片說明

我想要做的是從表 'order_items' 中選擇 'product_id' 和 'quantity',其中 'order_id' = 1

並使用從第一個查詢中提取的 'product_id',從表 'cards' 中選擇 *,其中 'prd_id' = 'product_id' 和 limit = 'quantity'。 是的,可能有多個 product_ids 。 誰能幫我寫一個快速的代碼? php mysqli 是首選。 謝謝

<?php    
const DB_SERVER = "localhost";
const DB_USER = "user_name";
const DB_PASSWORD = "password";
const DB = "db_name";
$conn=mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB); 
// $conn variable will hold the connection object 
// Get Product ids fro the order_items
$query="select product_id,quantity from order_items where order_id=1";
$result=mysqli_query($conn,$query);
$productIds=''; // Will be a string to append product ids
if(mysqli_num_rows($result) > 0)) 
{
   while ($row = mysqli_fetch_assoc($result)) {
      $productIds.=$row['product_id'].',';
   }
}
$productIds=rtrim($productIds,',');// Remove the last comma
// Once you get the product ids.
$query="select * from cards where prd_id in($productIds) limit 10";
// Limit should be exapmple - Limit 10 
$result=mysqli_query($conn,$query);
$data=array();
if(mysqli_num_rows($result) > 0)) 
{
   while ($row = mysqli_fetch_assoc($result)) {
      $data[]=$row;
   }
}
print_r($data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM