Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How safe is Php PDO function: lastInsertId?

Tags:

php

pdo

I have little confusion about the Php PDO function: lastInsertID. If I understand correctly, it returns the last auto-incremental id that was inserted in the database.

I usually use this function when I execute a query that inserts a user in my database when I am creating the functionality of registering a user.

My question is that say I have a hundred people registering on my site at one point for example. And may be one user hit the 'Register' button a millisecond after another user. Then is there a chance that this function lastInsertId will return the id of another user that register just momentarily earlier?

May be what I am trying to ask is does the server handle one request at a time and go through a php file one at a time?

Please let me know about this.

Thank you.

like image 778
Sameer Zahid Avatar asked May 10 '13 02:05

Sameer Zahid


People also ask

What is the advantage of PDO over Mysqlli?

Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.

How check PDO query is successful in php?

de.php.net/manual/en/pdostatement.execute.php says: Returns TRUE on success or FALSE on failure. - So bind it to a variable, e.g. $success = $STH->execute($params); and check that variable against true or false .

How does php PDO work?

PDO—PHP Data Objects—are a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.

What is PDO php extension?

PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).


1 Answers

Perfectly safe. There is no race condition. It only returns the last inserted Id from the pdo object that made the insert.

like image 174
eidsonator Avatar answered Oct 15 '22 11:10

eidsonator