我有2个表名称,每个名称均包含shipping_infos和订单。我想从2个表中获取多个列。 2个表共享同一列,即user_id列。我一直在尝试此代码,但是它仍然返回null。我应该怎么写?

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

最佳答案

试试这个查询:

$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."';";


并在这里更改:

    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],
));

10-05 19:57