简体   繁体   中英

How to get data from multiple columns from 2 tables in sql database?

I have 2 tables name each name shipping_infos and orders. I would like to get multiple columns out of the 2 tables. The 2 tables share one same column which is the user_id column. I have been trying on this code, however it still returns me null. How should i write it?

 <?php
  include ('classes/functions.php');

 if(isset($_POST['user_id'])){
 $user_id = $_POST['user_id'];
 $check_receipt = "select 
shipping_infos.shipping_name,shipping_infos.shipping_address,
shipping_infos.shipping_contact,shipping_infos.shipping_email,  
  orders.order_date,orders.trx_id,orders.tracking_num from
 shipping_infos inner join orders on shipping_infos.user_id = orders.user_id    
 where user_id= '".$user_id."';";
        $run_receipt_checking = mysqli_query($con, $check_receipt);
        $result = array();
    while($row = mysqli_fetch_array($run_receipt_checking)){
    array_push($result,
    array(
          'shipping_name'=>$row[2],
          'shipping_address'=>$row[3],
          'shipping_contact'=>$row[4],
          'shipping_email'=>$row[5],
          'order_date'=>$row[8],
          'trx_id'=>$row[1],
          'tracking_num'=>$row[2],            
));
}
    echo json_encode(array("result"=>$result));
} ?>

Try this query:

$check_receipt = "select  si.shipping_name,
        si.shipping_address,
        si.shipping_contact,
        si.shipping_email,
        o.order_date,
        o.trx_id,
        o.tracking_num 
from shipping_infos si
inner join orders o
    on si.user_id = o.user_id    
where si.user_id='".$user_id."';";

And change here:

    array_push($result,
    array(
          'shipping_name'=>$row[0],
          'shipping_address'=>$row[1],
          'shipping_contact'=>$row[2],
          'shipping_email'=>$row[3],
          'order_date'=>$row[4],
          'trx_id'=>$row[5],
          'tracking_num'=>$row[6],            
));

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