0

I am developing an web application where I have to give every new registrant a serial number. The main issue is with 'how to ensure uniqueness?'. I have searched through the different functions available with mysql and found mysql_insert_id() to be the fittest solution here. But before I run towards it, I need to know whether this function is thread-free. To more precise, say there are two users sitting at two different terminals and submits the registration form synchronously. Will they both get the same id out of the execution of the function mysql_insert_id()? Otherwise, my project will spoil. Please help. If I could not clear my point, please comment. Thanks in advance.

3 Answers 3

1

here is detailed solution

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
)

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. t What I want, in application, I will redirect the registrant to a success page with the facility to print the information like his/her name, application number(to be manufactured using the mysql_insert_id() )and other few details. Now, my question is that since those two concurrent users are viewing their details at the same time, will the two last inserted IDs conflict or overlap with each other. I want to carry forward the relevant ID during redirection to success page Will mysql_insert_id() ensure that? @ Rajat Jain.
to do this u have to carry some information like name and date of birth of that user to another page. then you have to make a sql query to fetch the unique id of that user for Eg: $result=mysqli_query($con, "select * from table where name='$name' and dob='$dob'");
0

If you have an id column on your table in your database and that column is set to be the primary key that will be enough. Even if 2 people will submit the form at the same the ids will be unique.

id column could be defined like this

ADD PRIMARY KEY (`id`)
id` int(11) NOT NULL AUTO_INCREMENT

2 Comments

Thanks @ Mateusz but I am looking forward to something else. Please read my comment above
@user3051467 the process would be as follows: on registration page do session_start() -> filled out form is being passed to the script that stores data -> last_insert_id is then stored into session variable -> you redirect to the confirmation page -> confirmation page checks for the last_insert_id in the session and runs a query to select data from the database based on that id
0

Alternatively, you can use the UUID() function in mysql.

A UUID is designed as a number that is globally unique in space and time. Two calls to UUID() are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other.

mysql> SELECT UUID(); -> '6ccd780c-baba-1026-9564-0040f4311e29'

For further details : http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid

1 Comment

Thanks @ alfallouji but I am looking forward to something else. Please read my comment above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.