如何从php中的SQL查询中获得一个值?

时间:2022-09-10 11:59:45

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.

如何使用php获得查询的第一个结果?我希望在列表按降序时返回的第一个结果。

$sql2 = "SELECT orderID FROM orders WHERE 'customerID' = '".$_SESSION['accountID']."' ORDER BY
                    'orderID' DESC";
                $lastorder = mysqli_query($sql2);

Thanks!

谢谢!

3 个解决方案

#1


3  

Simply add LIMIT 1 to the end of your SQL

只需在SQL末尾添加LIMIT 1

#2


0  

Use LIMIT:

使用限制:

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

#3


0  

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:

如果customerID是一个数字,那么可以删除单引号来进行查询:

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

#1


3  

Simply add LIMIT 1 to the end of your SQL

只需在SQL末尾添加LIMIT 1

#2


0  

Use LIMIT:

使用限制:

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

#3


0  

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:

如果customerID是一个数字,那么可以删除单引号来进行查询:

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