如何使用php獲取此查詢的第一個結果?我想要列表按降序返回的第一個結果。如何從php中的SQL查詢中獲取一個值?
$sql2 = "SELECT orderID FROM orders WHERE 'customerID' = '".$_SESSION['accountID']."' ORDER BY
'orderID' DESC";
$lastorder = mysqli_query($sql2);
謝謝!
如何使用php獲取此查詢的第一個結果?我想要列表按降序返回的第一個結果。如何從php中的SQL查詢中獲取一個值?
$sql2 = "SELECT orderID FROM orders WHERE 'customerID' = '".$_SESSION['accountID']."' ORDER BY
'orderID' DESC";
$lastorder = mysqli_query($sql2);
謝謝!
只需添加LIMIT 1
到您的SQL結束
使用LIMIT
:
'SELECT orderID FROM orders WHERE customerID = "' . $_SESSION['accountID'] . '" ORDER BY orderID DESC LIMIT 1'
查詢:
$query = "SELECT MAX(orderID) as orderID
FROM orders
WHERE customerID = '" . $_SESSION['accountID'] . "'";
如果的customerID是一個數字,則單引號可以拆下來查詢:
$query = "SELECT MAX(orderID) as orderID
FROM orders
WHERE customerID = " . $_SESSION['accountID'];
然後...
// include database_link since you are not using OO-style call.
$result = mysqli_query($database_link, $query);
$row = $result->fetch_object();
$orderID = $row->orderID;