此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我认为如果它们按降序从表中拉出,它们将以相同的顺序存储在数组中。
我想错了吗?
答案 0 :(得分:0)
错误很可能是您如何迭代结果。用mysql_fetch_assoc()
替换while循环,它应该可以工作:
while ($row = mysql_fetch_assoc($nresult)) {
echo $row['id'];
// ...
}
答案 1 :(得分:0)
这样做了:
$key = "ID";
array_multisort($nresult, SORT_DESC, $key);
来自:
How to sort an array of associative arrays by value of a given key in PHP?