简体   繁体   中英

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. 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.

Thanks (sorry for my bad english by the way)

Use LIMIT with ORDER BY

SELECT *
FROM news
ORDER BY post_date DESC
LIMIT 5

to get the 5 latest news items.


As for inserting. Use a PRIMARY KEY with AUTOINCREMENT to automatically get sequential id's for your news items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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