Search code examples
phpmysqlauto-incrementfile-storage

Fetch the auto increment id from mysql database


I have a database with an Auto increment field. I want to retrieve the value of the Auto increment column whenever i insert a record in database. I am using mysql as my database and PHP as my programming language. I tried using mysql_insert_id() function but then it is not suitable when there are large number of insertion taking place at same point of time.

I want to retrieve the value of AI column because i am storing a file for respective record and since i am storing the files in a single directory i need the file name to be unique. Any other way to do the same thing?


Solution

  • It should be safe for you to use mysql_insert_id(), because it will return the last inserted AI value of the current connection (see php.net). So as long as you call the method right after the INSERT query in your script, there won't be any race conditions, even if your script is running concurrently multiple times.