Jump to content

Archived

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

lance

Query last 20 entries

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.

Share this post


Link to post
Share on other sites

use the limit feature at the end of your query. e.g.: select * from table limit 20

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.