
The: 28/09/2008 - AT: 9:10
My old method of getting the last inserted ID was insecure, atleast when it comes to scripts with concurrent users.
Many developers are incorrectly performing a select query, to get the auto_increment value. And then they add one to the value returned. We however cant rely on this, since it isn't 100% certain that we get the number we want/expect.
So what we can do, is use the mysql_insert_id function of PHP on out database link, (aka connection).
// Getting Last Inserted Row $last_inserted_row = mysql_insert_id($dblink);
To return the last inserted row, which was inserted by the given connection.
The $dblink is unique to the running script, this should make it impossible to get the wrong id returned, in case another user inserted a row in the middle.
We simply update the Temp-row, including the now known auto_increment value in whatever field we need it. (In my case the filename field, which is my reference to the uploaded file).
I'm currently working on a photogallery for my Blog, where i needed to know the last inserted Id, so i easily could assign the uploaded files a unique value.
Comments: [0]
Author: BlueBoden
1: Use [code][/code] for right code examples.
2: Use [code2][/code2] for wrong examples.
3: Use [h][/h] for secondary headlines.
4: Use [strong][/strong] for strong text.
5: Use [url=http://www.yoursite.com/]TITLE[/url] for links.
Welcome Guest
Copyright © Brugbart