doushang2571 2017-12-06 03:29
浏览 84
已采纳

如何从两个调用表(函数)中获取数据两次使用true还是false?

I have a function, responsible for knowing if a user made the purchase correctly (complete) or if the status of the purchase is (incomplete).

This function is with a single query.

function checkOrderComplete($con,$id_product,$id_user){
  $res = false; // the default result
  $stmt = $con->prepare("SELECT status FROM order WHERE id_product=? AND id_user=? AND status=? limit 1"); 
  $stmt->bind_param("iis",$id_product,$id_user,$status); 
  $status='complete'; 
  $stmt->execute();

  $stmt->store_result(); 

  if ($stmt->num_rows ===1){
    $res = true;
  } else {
    $res = false;
  }
  return $res;

}

Now I have added a new table called order_details

id_order_product  order_date  id_product  quantity  price   id_order
       1             -----          2        1       10.00     18
       2             -----          6        1       50.00     18

Order

id_order id_user  status   
  18        3    complete  

So what I want to achieve is the same functioning of the function you can realize before, it took into account the status of the purchase if it was completed or not, and the id of the product, and the id of the user who made the purchase.

Then perform the following procedure but it shows me errors in the $stmt->bind_param("isi",$id_user,$status,$id_product);

The query:

$stmt = $con->prepare("SELECT order.status, order.id_user, order_details.id_product FROM order inner join order_details ON order.id_user=? order.status=? order_details.id_product=? LIMIT 1");

How do I obtain the same values mentioned from the two tables?

  • 写回答

1条回答 默认 最新

  • douhe4336 2017-12-06 07:56
    关注

    Your JOIN is not not properly formatted. The way to define a JOIN is to use the ON keyword to define what fields should be equal to perform the JOIN. Like so:

    SELECT order.status,
           order.id_user,
           order_details.id_product
    FROM order
    INNER JOIN order_details ON order_details.id_order = order.id_order
    WHERE order.id_user = ?
      AND order.status = ?
      AND order_details.id_product = ?
    

    Then bind your parameters as required.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?