[英]Getting the Previous Entry From A Database
I'm building a news website with articles and I'm trying to show the next and previous articles at the end of each post. 我正在建立一个包含文章的新闻网站,并且试图在每篇文章的末尾显示下一篇和上一篇文章。
The next link is working fine but the previous link always shows the first ever post. 下一个链接工作正常,但上一个链接始终显示有史以来的第一篇文章。 Here is the
get
code that I'm using for each: 这是我正在使用的
get
代码:
$get_next_sql = "SELECT * FROM articles WHERE id > '$article_id' LIMIT 1";
$get_prev_sql = "SELECT * FROM articles WHERE id < '$article_id' LIMIT 1";
Am I missing something in the prev
code? 我缺少的东西
prev
代码?
You are getting only posts which have id
less than current. 您只会收到
id
小于当前id
帖子。
By default, Database is sorting with PRIMARY field and IN ASC order. 默认情况下,数据库按PRIMARY字段和IN ASC顺序排序。
So, its always returning post with lowest id
. 因此,其始终返回的
id
最低的帖子。
What you need is the post lower than current and with highest id
. 您需要的是低于当前且
id
最高的帖子。
So, the corrected code should be: 因此,更正后的代码应为:
$get_prev_sql = "SELECT * FROM articles WHERE id < '$article_id' ORDER BY id DESC LIMIT 1";
$get_prev_sql = "SELECT * FROM articles WHERE id < '$article_id' ORDER BY id DESC LIMIT 1";
主键的默认顺序是ASC
使用此查询
select * from table_name where id < your_current_id order BY id desc limit 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.