Jump to content

Posting recent data from MySQL Database


huntrguy102

Recommended Posts

I have a database full of information of books (I.D.#, title, author, genre, publisher). What I need is for the 3 most recent books in the database to be posted on the website.  Is there a way to use the I.D. numbers to post the most recent, so I could just add more books and it would update itself to the most recent. They are ordered 1,2,3,4 etc. the highest numbers being the most recent.  Any help would be greatly appreciated.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/194513-posting-recent-data-from-mysql-database/
Share on other sites

If your book table's primary key is a field called "id", then something like this will do it:

 

SELECT * FROM books
ORDER BY id DESC
LIMIT 3

 

With this SQL, the most recent book will be the last one in the results. You could get the most recent book to appear at the top of the results like this:

 

SELECT * FROM (
SELECT * FROM books
ORDER BY id DESC
LIMIT 3
) x
order by id ASC
;

 

Or you could just loop through it backwards in the PHP :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.