我正在尝试通过DESC订购我的网站的新闻部分,以便我可以先显示最新的帖子,但是当我添加ORDER BY DESC时,我收到此错误:
警告:mysql_fetch_assoc()要求参数1为资源,在第83行的C:\ xampp \ htdocs \ DeathRace \ site \ index.php中给出布尔值
我不知道为什么我收到这个错误,但这是非常不方便的,如果有人可以帮助我,我将不胜感激,这是我试图循环的代码:
$getnews = mysql_query("SELECT * FROM news LIMIT 0, 5 ORDER BY DESC id");
$per_page = 5;
while($row = mysql_fetch_assoc($getnews))
{
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
$postedby = $row['postedby'];
echo
"
$title posted on" .date('d-m-Y' ,strtotime($date))."$body By user: $postedby <br/>
";
}
答案 0 :(得分:4)
SELECT * FROM news ORDER BY id desc limit 5
修改。如果要以其他格式显示mysql日期,例如d-m-Y,可以在查询中使用date_format()函数
select field1,field2,....,date_format(your_date,'%d-%m-%Y') as your_date from ...
答案 1 :(得分:0)
$sql = "Select field1, field2 from table1 where field1 = 'something' order by field2 desc limit 0,5";
答案 2 :(得分:0)
您需要更改为查询的查询中存在错误
SELECT *
FROM news
ORDER BY id DESC
LIMIT 0 , 5
这将为您提供表格中的最后5条记录