gpt4 book ai didi

php - PDO ORDER BY 不适用于非自动增量的字段

转载 作者:行者123 更新时间:2023-11-29 12:06:19 24 4
gpt4 key购买 nike

这是我的代码:

$q = $db->prepare("SELECT * from slider WHERE page = :page ORDER BY index ASC");
$q->bindValue(':page', basename($_SERVER['PHP_SELF']));
$q->execute();

if ($q->rowCount() > 0){
$result = $q->fetchAll(PDO::FETCH_ASSOC);

for ($i=0; $i < $q->rowCount(); $i++) {
$path = $result[$i]['path'];
echo "<div><img src=\"$path\"></div>";
}
}

目前,没有显示任何结果。但是,如果我按不同的字段(即自动增量的“id”和表的主键)排序,我确实会得到按正确字段排序的结果。

最佳答案

'index ' 是 MySQL 中的保留字。如果您想将其用作列名,并且在查询中,您必须用反勾号将其括起来。

SELECT * from `slider` WHERE `page` = :page ORDER BY `index` ASC

关于php - PDO ORDER BY 不适用于非自动增量的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31412738/

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