开发者

How do I get one value from an SQL query in php?

开发者 https://www.devze.com 2023-03-10 05:56 出处:网络
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.

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!


Simply add LIMIT 1 to the end of your SQL


Use LIMIT:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号