简体   繁体   中英

PHP Mysqli Help Selecting from multiple tables

So.. I have two mysqli tables like this :

First one Table name : order items

在此处输入图片说明

And 2nd one : Table name : cards

在此处输入图片说明

What I want to do is to select 'product_id' and 'quantity' from table 'order_items' where 'order_id' = 1

And using the 'product_id' extracted from the 1st query , select * from table 'cards' where 'prd_id' = 'product_id' and limit = 'quantity' . yes there might be multiple product_ids . Can anyone write a quick code for me? php mysqli is preferred. Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM