How to get single value from SQL query in php?

How to get the first result of this query using php? I want the first result to be 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);

Thank!

+3
source share
3 answers

Just add LIMIT 1at the end of your SQL

+3
source

Use LIMIT:

'SELECT orderID FROM orders WHERE customerID = "' . $_SESSION['accountID'] . '" ORDER BY orderID DESC LIMIT 1'
0
source

Inquiry:

$query = "SELECT MAX(orderID) as orderID
            FROM orders 
            WHERE customerID = '" . $_SESSION['accountID'] . "'";

If customerID is a number, single quotes can be removed to make a request:

$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;
0
source

All Articles