Selecting the Latest Row from a MySQL Table
I am no programmer that is for sure, but when you are the head of marketing, sales, development, and IT at your company, (we call that an internet marketer) you have to figure things out.
So I have a site that is run off of a bunch of MySQL tables. I wanted to display the latest entry from a few specific tables on the front page of the site. I could pay someone $150 and give them access to my mortgage payment or I can figure it out myself. I decided on the later.
So here it is:
Lets assume that you have a table named “entries” and that the primary key is “id.” Every time you make a new entry, the entry gets an id number higher than the last. What we want is the highest number “id” and its data to be displayed. So what I discovered is that this is a two step process.
- Get the information
- Display the information
At some point in the beginning of your php page you have to connect to the database. If you are working on an existing site it is probably already at the top of the page.
Step 1 Get the information
I used this to get the latest entry:
<?$sql = "SELECT * FROM entries ORDER BY id DESC LIMIT 1";$rs = $db->Execute( $sql );?>
Then I had to display the info:
<h2><?=$rs->fields['title']?></h2> <?=$rs->fields['thumbnail']?>
I hope this helps someone in the future.
Posted by Carl Thomas / Affiliate Preacher on July 30th, 2007









1 Response to: "Selecting the Latest Row from a MySQL Table"