PDO + mySQL的订单不起作用

时间:2013-12-30 14:04:38

标签: php mysql pdo

我想将最后一行插入数据库,但只获取第一行。我怎么解决它?

在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.'';

1 个答案:

答案 0 :(得分:1)

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

查询应该是:

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

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