sted999 Posted April 14, 2009 Share Posted April 14, 2009 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()); ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 That looks correct. What type is date? Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 14, 2009 Author Share Posted April 14, 2009 its a date type in the database, would you suggest changing this to another field? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 14, 2009 Share Posted April 14, 2009 try using ASC instead .. using LIMIT on a query will grab the first row every time .. gotta use some reverse psychology on it. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2009 Share Posted April 14, 2009 Post the whole row in the table that it is returning and post the whole row that you expect it to return. Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 14, 2009 Author Share Posted April 14, 2009 For some reason changing the order by field effects the result. Is ORDER usually used to order by a field e.g. date created which has a timestamp? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 14, 2009 Share Posted April 14, 2009 i read 'latest' as 'last' .. i need a break. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 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". Quote Link to comment Share on other sites More sharing options...
revraz Posted April 14, 2009 Share Posted April 14, 2009 Again, post the Data that the row contains so we can see it. Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 14, 2009 Author Share Posted April 14, 2009 Sorry for the confusion, I dont think I really understood it in the first place. Its all sorted now. Sorry again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.