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
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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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