如何从sql数据库中的2个表中的多列中获取数据?

时间:2016-04-03 09:05:00

标签: php mysql sql mysqli

我有2个表名称,每个名称shipping_infos和订单。我想从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));
} ?>

1 个答案:

答案 0 :(得分:0)

尝试此查询:

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

并在此处更改:

{{1}}