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.
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.
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 .
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.
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).
Perfectly safe. There is no race condition. It only returns the last inserted Id from the pdo object that made the insert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With