skip to Main Content

i want to get the order id maximum by the relative user id

$order_id = "select * from products_orders where order_user_id = $user_id AND MAX(order_id) ";
$ord_del = mysqli_query($con,$order_id) ;
$ord_row = mysqli_fetch_assoc($ord_del);

i try this method but this wont work

2

Answers


  1. The MAX SQL command is a group function which needs to indicate with the common columns. So just adding the GROUP BY should work as you want.

    $order_id = "SELECT order_user_id,order_id FROM products_orders WHERE order_user_id = $user_id AND MAX(order_id) GROUP BY order_user_id";
    
    Login or Signup to reply.
  2. You may amend the select statement so that it is order by order_id desc and then just retrieve the 1st record (limit 0,1)

    In that case only the record with the max order_id will be retrieved.

    So based on your original code, you may use

    $order_id = "select * from products_orders where order_user_id =". $user_id . " order by order_id desc limit 0,1";
    $ord_del = mysqli_query($con,$order_id) ; 
    $ord_row = mysqli_fetch_assoc($ord_del);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search