简体   繁体   English

如何在PHP中包含来自MYSQL的前5个表行

[英]How to include first 5 table rows from MYSQL in PHP

I dont know if what i want is possible but i ask it anyway :) I want to make a "news" container on my website. 我不知道我想要的是否可能,但无论如何我都会问:)我想在我的网站上制作一个“新闻”容器。 so if i add a new article than the oldest article must disappear. 所以,如果我添加一篇新文章,那么最旧的文章必须消失。 i thought, maybe it is possible to include only the first 5 rows of the database. 我想,也许只能包含数据库的前5行。 And if that can be done then i have to insert the newest article on the first row of the database. 如果可以这样做,那么我必须在数据库的第一行插入最新的文章。

I hope that someone has a script that inserts a new article on the first row of my table AND a script that includes only the first 5 rows of my table. 我希望有人有一个脚本在我的表的第一行插入一篇新文章和一个只包含我表的前5行的脚本。

Thanks (sorry for my bad english by the way) 谢谢(顺便说一句,对不起我的英语不好)

Use LIMIT with ORDER BY 使用LIMITORDER BY

SELECT *
FROM news
ORDER BY post_date DESC
LIMIT 5

to get the 5 latest news items. 获取5个最新新闻。


As for inserting. 至于插入。 Use a PRIMARY KEY with AUTOINCREMENT to automatically get sequential id's for your news items. 使用带有AUTOINCREMENTPRIMARY KEY可以自动获取新闻项的顺序ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM