Reputation: 2172
How do I get the first result of this query using php? I want the first result that is returned when the list is in descending order.
$sql2 = "SELECT orderID FROM orders WHERE 'customerID' = '".$_SESSION['accountID']."' ORDER BY
'orderID' DESC";
$lastorder = mysqli_query($sql2);
Thanks!
Upvotes: 3
Views: 6629
Reputation: 2030
The query:
$query = "SELECT MAX(orderID) as orderID
FROM orders
WHERE customerID = '" . $_SESSION['accountID'] . "'";
If customerID is a number then the single quotes can be removed to make the query:
$query = "SELECT MAX(orderID) as orderID
FROM orders
WHERE customerID = " . $_SESSION['accountID'];
Then...
// include database_link since you are not using OO-style call.
$result = mysqli_query($database_link, $query);
$row = $result->fetch_object();
$orderID = $row->orderID;
Upvotes: 0
Reputation: 7131
Use LIMIT
:
'SELECT orderID FROM orders WHERE customerID = "' . $_SESSION['accountID'] . '" ORDER BY orderID DESC LIMIT 1'
Upvotes: 0