ChrisMartino Posted April 16, 2010 Share Posted April 16, 2010 Hello there, I'm wondering if somebody can help me, I am trying to retrieve the last added record from a MySQL database so i can get information from it, But i don't know how to get the last record, Just the last added record to the database, Dose anybody know how to do this?. Thanks for your time, Chris. Link to comment https://forums.phpfreaks.com/topic/198779-getting-the-lasted-added-record-from-mysql/ Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 If it has an primary key ID field, try: SELECT * FROM tablename ORDER BY id DESC LIMIT 1; Link to comment https://forums.phpfreaks.com/topic/198779-getting-the-lasted-added-record-from-mysql/#findComment-1043254 Share on other sites More sharing options...
DWilliams Posted April 16, 2010 Share Posted April 16, 2010 It might be worth checking out mysql_insert_id at http://php.net/manual/en/function.mysql-insert-id.php I'm not sure if it only returns an ID if your script was the one actually doing the inserting or not, though. EDIT: Disregard, it only appears to work on queries executed within your same script execution Link to comment https://forums.phpfreaks.com/topic/198779-getting-the-lasted-added-record-from-mysql/#findComment-1043274 Share on other sites More sharing options...
Mchl Posted April 16, 2010 Share Posted April 16, 2010 Using query Ken2k7 gave you you will indeed get last record in the table on the assumption, that the record with highest ID is the latest. This is not always true however, so if you really want to be sure, you should be timestamping your rows. mysql_insert_id() (or MySQL's LAST_INSERT_ID() ) functions will only give you an ID of last record insert during current connection and even this only if it was a single row insert. See MySQL's manual for more information. Link to comment https://forums.phpfreaks.com/topic/198779-getting-the-lasted-added-record-from-mysql/#findComment-1043281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.