user3049504
user3049504

Reputation: 1

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)

Upvotes: 0

Views: 69

Answers (1)

Halcyon
Halcyon

Reputation: 57709

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.

Upvotes: 2

Related Questions