Jump to content

Query last 20 entries


lance

Recommended Posts

Or 10 or whatever number you want to display (not all). I\'m new to all this wonderful PHP MySQL stuff and would like to display a table of the last 20 entries made. To be more specific it\'s a PIREP reporting system for pilots to submit their flight hours, destination, etc...

 

I can\'t display all, but would like to be able to display the last 20 or so pireps in a table.

 

I AM Able to query the database and display ALL records, but that page will be come waaaaaaaaaay to large very fast ;-)

 

Thanks for any help or any links you could provide to tutorials that might help me understand how to do this.

Link to comment
Share on other sites

just to expand of effigy\'s post you can do this.

 

select count(*) from table;

 

and stick it in a variable called $max ... and subtract 20 from it.

 

then make your query string

\"select * from table limit $max,-1\"

 

which should give you the last 20 lines of the table.

 

you may want to stipulate an order by clause on a datetime field or unique incremental to make sure the data is the last 20 entries.

Link to comment
Share on other sites

Mysql\'s limit feature is one of the really nice things it offers. That\'s definately the way to go. Hopefully you have a key to the table using auto_increment, or have a timestamp column. Then you would not have to bother with trying to get the count(*) and do a limit count(*)-20, and would instead simply do:

 

SELECT * FROM Table ORDER BY Table_id DESC LIMIT 20;

 

This will display the rows most recent first. May be what you want, and saves an extra query.

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.