Home » Php » How safe is Php PDO function: lastInsertId?

How safe is Php PDO function: lastInsertId?

Posted by: admin July 12, 2020 Leave a comment


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.

How to&Answers:

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


It is safe – it guarantees to return you a value from the current connection.