DWilliams Posted August 13, 2011 Share Posted August 13, 2011 ...to be the latest entry? If I want to get the ID of the last inserted row, can I just order by the PK and limit 1? I know about mysql_insert_id() and similar functions, but I need to do this from a separate script execution and those must be run directly after the insert. Quote Link to comment https://forums.phpfreaks.com/topic/244670-if-the-pk-of-a-table-is-set-to-auto-increment-can-i-trust-the-highest-id/ Share on other sites More sharing options...
joel24 Posted August 13, 2011 Share Posted August 13, 2011 yes, the highest id will be the latest. Quote Link to comment https://forums.phpfreaks.com/topic/244670-if-the-pk-of-a-table-is-set-to-auto-increment-can-i-trust-the-highest-id/#findComment-1256734 Share on other sites More sharing options...
PFMaBiSmAd Posted August 13, 2011 Share Posted August 13, 2011 You cannot guarantee that the highest id will be the id that any invocation of a script just inserted because concurrent visitors can each cause their own id to be inserted. You will end up cross-linking information using the wrong id. How are you 'executing' these separate scripts that need to use the correct id and why don't you have all the related processing on one page? Quote Link to comment https://forums.phpfreaks.com/topic/244670-if-the-pk-of-a-table-is-set-to-auto-increment-can-i-trust-the-highest-id/#findComment-1256783 Share on other sites More sharing options...
DWilliams Posted August 13, 2011 Author Share Posted August 13, 2011 You cannot guarantee that the highest id will be the id that any invocation of a script just inserted because concurrent visitors can each cause their own id to be inserted. You will end up cross-linking information using the wrong id. How are you 'executing' these separate scripts that need to use the correct id and why don't you have all the related processing on one page? I see what you're saying but luckily in my case, the script that does the inserting is executed by cron and not by visitors. In that case, it should be fine, right? The script will only run every 30 minutes or so. The script in question is connecting to a third party API and processing all new entries that have been added since the last script execution. Each remote entry has an ID, so I store IDs that have already been processed to ensure that they don't get processed again. Ideally my script will keep parsing new entries until it hits one with an ID equal to the most recently inserted ID in my database. Quote Link to comment https://forums.phpfreaks.com/topic/244670-if-the-pk-of-a-table-is-set-to-auto-increment-can-i-trust-the-highest-id/#findComment-1256828 Share on other sites More sharing options...
fenway Posted August 14, 2011 Share Posted August 14, 2011 Why not store a processed date field? Then the ones that are NULL are new. Quote Link to comment https://forums.phpfreaks.com/topic/244670-if-the-pk-of-a-table-is-set-to-auto-increment-can-i-trust-the-highest-id/#findComment-1257192 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.