如何从 PHP 中的 SQL 查询中获取一个值?

发布于 2024-11-13 23:11:20 字数 269 浏览 1 评论 0原文

如何使用 php 获取此查询的第一个结果?我想要当列表按降序排列时返回的第一个结果。

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-11-20 23:11:20

只需将 LIMIT 1 添加到 SQL 末尾

Simply add LIMIT 1 to the end of your SQL

心病无药医 2024-11-20 23:11:20

使用LIMIT

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

Use LIMIT:

'SELECT orderID FROM orders WHERE customerID = "' . $_SESSION['accountID'] . '" ORDER BY orderID DESC LIMIT 1'
蝶舞 2024-11-20 23:11:20

查询:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文