Jump to content

[SOLVED] Selecting latest record from database


sted999

Recommended Posts

Hiya all.

What I am trying to do is select the last entry to my mysql table. I searched the internet and found an example, but this seems to be picking out the first entry. Can anybody suggest how to easily get the last(latest) entry. I have looked on the net but cant find anything?

 

Thanks.

 

<?php
$query = mysql_query("SELECT startTown,destinationTown FROM Journey ORDER BY date DESC LIMIT 1") 
                    or die(mysql_error());
?> 

Link to comment
Share on other sites

try using ASC instead ..

 

No, that would list the data from past to present.

 

using LIMIT on a query will grab the first row every time ..

 

Yes, exactly.  The first row after it's been ordered by descending date.

 

@OP. you mean the most recent entry, correct?  Than the query you have is correct, something else is wrong.

Link to comment
Share on other sites

Yes, it's pretty self explanatory.

 

ORDER BY date DESC LIMIT 1

 

=

 

order by date(column) in descending order only grab 1 record.

 

i read 'latest' as 'last' .. i need a break.

 

So did I, that's why I first thought it was ASC but then realized he meant "most recent".

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.