Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get autoincrement before inserting a new row

Status
Not open for further replies.

JRBeltman

IS-IT--Management
Feb 5, 2004
290
NL
Hi,
how can I get the next auto increment number from a table before inserting a row?

Using max(id) wouldn't work anymore if I had just deleted the last record as the 'id' returned would be 1 less than the auto increment. Any suggestings?

JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
Insufficient data for a meaningful answer.

What database system are you using?


In general, any calculation involving getting the maximum of an id column should be suspect. Web apps are multiuser apps, so there is no guarantee that as one instantiation of a script is calculating the "next" value, another instantiation of the script has already inserted a record with that value.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Yes, I am so used to it that I don't even think about the system I am using.

Win xp with LAMP(-L :)

But, from your answer I conclude that I better first insert an empty record, then get the last_insert_id() and update that record... I need that id as some values of the record to be inserted use it..


JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
good point actually.
Ah, right. I remember. It is because I am uploading some thumbnails and I wanted to rename these to the auto increment id + extension. The extension may differ, so I could get away with storing that only as the id will be indeed the same as the auto inc id. Saves a little space and time.



JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
Right. And since MySQL tracks auto_increment IDs on a per-connecion basis, you can insert the record, fetch the a_i ID, then rename your thumbnails as appropriate.

Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top