gpt4 book ai didi

php - 通过 PDO+mySQL 排序不起作用

转载 作者:行者123 更新时间:2023-11-29 13:21:03 24 4
gpt4 key购买 nike

我想将最后一行插入数据库,但只获取第一行。我该如何修复它?

在 phpmyadmin 中一切正常: phpmyadmin但代码不起作用:

Último precio: 28.41

代码:

            $skip = 0;
$max = 1;
$id = 'gooo_id';
$q = $conn->prepare('SELECT * FROM price WHERE asin = :asin ORDER BY :id DESC LIMIT :skip, :max');
$q->bindValue(':asin', $asin, PDO::PARAM_STR);
$q->bindValue(':id', $id, PDO::PARAM_STR);
$q->bindValue(':skip', (int) trim($skip), PDO::PARAM_INT);
$q->bindValue(':max', (int) trim($max), PDO::PARAM_INT);
$q->execute();
$result_row = $q->fetchObject();
$lastprice = $result_row->price;
echo 'Último precio: '.$lastprice.'';
<小时/>

解决方案

$skip = 0;
$max = 1;
$q = $conn->prepare('SELECT * FROM price WHERE asin = :asin ORDER BY gooo_id DESC LIMIT :skip, :max');
$q->bindValue(':asin', $asin, PDO::PARAM_STR);
$q->bindValue(':skip', (int) trim($skip), PDO::PARAM_INT);
$q->bindValue(':max', (int) trim($max), PDO::PARAM_INT);
$q->execute();
$result_row = $q->fetchObject();
$lastprice = $result_row->price;
echo 'Último precio: '.$lastprice.'';

最佳答案

您需要在查询中的 ORDER BY 之后使用 SQL 表中的属性名称。您输入的值作为参数,该值对于所有行都相同。因此它不会对订单执行任何操作。

查询应该类似于:

$q = $conn->prepare('SELECT * FROM price WHERE asin = :asin ORDER BY gooo_id DESC LIMIT :skip, :max');

当然你需要调整绑定(bind)变量...

关于php - 通过 PDO+mySQL 排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com