I google the mysql_insert_id, about the thread safe concern... ...I am worrying about I only have a one MySQL user to connect DB, so, I think mysql_insert_id is not very safe for my application. Is there any "safer" method for returning last insert id from MySQL? thank you.
As long as no two threads share one database connection you should not get any wrong ids. mysql_insert_id is stored per connection, not per user.
There is no safer method and you don't need any. mysql_insert_id() in PHP or SELECT LAST_INSERT_ID() returns the last ID per your current connection, not per user account, so you are perfectly safe here.
Related
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.
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.
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.
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.
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.
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.
I'm using the mysql_insert_id within my code to get an auto increment.
I have read around and it looks like there is no race condition regarding this for different user connections, but what about the same user? Will I be likely to run into race condition problems when connecting to the database using the same username/user but still from different connection sessions?
My application is PHP. When a user submits a web request my PHP executes code and for that particular request/connection session I keep a persistent SQL connection open in to MySQL for the length of that request. Will this cause me any race condition problems?
None for any practical purpose, If you execute the last_id request right after executing your insert then there is practically not enough time for another insert to spoil that. Theoretically might be
possible
According to PHP Manual
Note:
Because mysql_insert_id() acts on the last performed query, be sure to
call mysql_insert_id() immediately after the query that generates the
value.
Just in case you want to double check you can use this function to confirm your previous query
mysql_info
The use of persistent connections doesn't mean that every request will use the same connection. It means that each apache thread will have its own connection that is shared between all requests executing on that thread.
The requests will run serially (one after another) which means that the same persistent connection will not be used by two threads running at the same time.
Because of this, your last_insert_id value will be safe, but be sure that you check the result of your inserts before using it, because it will return the last_insert_id of the last successful INSERT, even if it wasn't the last executed INSERT.
I've been using mysql_pconnect to establish all DB connections on my PHP site, with the theory that it's more efficient (debatable, I know).
I went to use mysql_insert_id to get the ID from a recent INSERT and it occurred to me that given the multi-threaded nature of web requests, I can't guarantee that another PHP script using the same pconnection has made a DB INSERT before my call to mysql_insert_id.
This is kind of a huge deal as I see no other way to guarantee atomicity of the INSERT and ID retrieval, as the ID is not returned by the INSERT query (or I don't know how to get it).
So basically I can never use mysql_pconnect if I want to have thread-safe INSERTS and ID retrieval?
mysql_insert_id() returns the last id within context of the current connection session. Thus avoiding race-condition problems. There's a bunch of notes/comments regarding this on the php.net manual for mysql_insert_id()
More Info:
PHP/MySQL insert row then get 'id'
How bad is using SELECT MAX(id) in MYSQL instead of mysql_insert_id() in PHP?
get id of last inserted record without using mysql_insert_id()
This should be easy to test:
<?php
// connection already established
mysql_query("INSERT INTO table VALUES('foo', 'bar')");
sleep(15);
echo mysql_insert_id();
Then see if you can fool it by slamming it with requests during the sleep period.. and check the insert_id results.
mysql_pconnect means that the connection won't be closed from the php module to the mysql.As there is an overhead of creating a new connection so it can be beneficial if you are creating a lot of connections.
As php executes a request in a single thread.And most probably you will be using 1 connection to the database a call to INSERT and subsequent call to ID retrieval is gonna be ATOMIC.
The MYSQL connection taken by 1 php thread can't be used by another php thread while it is still being in used which ensure atomicity.