[英]SORT BY in MySQL query does not change the order in the query results array
這個MySQL查詢
SELECT * FROM notes WHERE school_code = 'brisch' ORDER BY ID DESC
在MySQL終端中工作(以降序ID記錄顯示記錄在表中的升序排列),但是當我在PHP腳本中以如下方式使用它時:
$query = "SELECT * FROM notes WHERE school_code = '$skcode' ORDER BY ID DESC";
$nresult = mysql_query($query);
$nnum = mysql_numrows($nresult);
然后使用以下命令輸出行:
$i=0;
while ($i < $nnum)
{
// take each row from the array $nresult
$i++;
}
它以表順序顯示行,即通過升序排列ID,我認為如果它們以降序從表中拉出,它們將以相同順序存儲在數組中。
我認為這是錯誤的嗎?
該錯誤很可能是您遍歷結果的方式。 用mysql_fetch_assoc()
替換while循環,它應該可以工作:
while ($row = mysql_fetch_assoc($nresult)) {
echo $row['id'];
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.