This is a discussion on What if two or more users run the queries at the same moment? within the MySQL Database forums, part of the Database Forums category; I'm using PHP with the PDO functions to access to a MySQL database. I've a table 'Projects' with ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm using PHP with the PDO functions to access to a MySQL database. I've a table 'Projects' with an autoincrementing 'id' and a table 'Categories' which stores the categories the project belongs to. When I've to insert a new project into the db, I run this query: I) INSERT INTO Projects (id, ...) VALUES (NULL, ...); Then I take the id inserted using PDO::lastInsertID() and run this one: II) INSERT INTO Categories (id_project, ...) VALUES ($last_id, ...); My question is: what if two or more users will submit a new project at the "same" moment? Let's imagine this is the temporal sequence: 1) the first one runs the (I) query (eg. new ID = 10); 2) the second one runs the (I) query (eg. ID = 11); 3) the first one runs the (II) query, using PDO::lastInsertID(); 4) the second one runs the (II) query, using PDO::lastInsertID(); Will PDO::lastInsertID() return 11 or (correctly) 10 at the moment #3? In general, which is the best way to make a set of queries be executed in a "serial" way? I'd like that the first user runs the (I) and the (II) queries and only at the end the second user will start running the (I) query. Is it possible to do that? Thanks! Giovanni ps. I'm already using PDO::beginTransaction() and PDO::commit(). |
|
|||
|
On 5 Jun, 14:21, "Giovanni R." <gie...@askme.it> wrote:
> I'm using PHP with the PDO functions to access to a MySQL database. > > I've a table 'Projects' with an autoincrementing 'id' and a table > 'Categories' which stores the categories the project belongs to. > > When I've to insert a new project into the db, I run this query: > > I) INSERT INTO Projects (id, ...) VALUES (NULL, ...); > > Then I take the id inserted using PDO::lastInsertID() and run this one: > > II) INSERT INTO Categories (id_project, ...) VALUES ($last_id, ...); > > My question is: what if two or more users will submit a new project at > the "same" moment? > > Let's imagine this is the temporal sequence: > > 1) the first one runs the (I) query (eg. new ID = 10); > 2) the second one runs the (I) query (eg. ID = 11); > 3) the first one runs the (II) query, using PDO::lastInsertID(); > 4) the second one runs the (II) query, using PDO::lastInsertID(); > > Will PDO::lastInsertID() return 11 or (correctly) 10 at the moment #3? > > In general, which is the best way to make a set of queries be executed > in a "serial" way? > > I'd like that the first user runs the (I) and the (II) queries and only > at the end the second user will start running the (I) query. > > Is it possible to do that? > > Thanks! > Giovanni > > ps. I'm already using PDO::beginTransaction() and PDO::commit(). The second user will be running with a different connection. They will get their last insert id, you will get yours. |
|
|||
|
On 5 Jun, 15:35, "Giovanni R." <gie...@askme.it> wrote:
> Captain Paralytic wrote: > > The second user will be running with a different connection. They will > > get their last insert id, you will get yours. > > Ok, I'll trust you. :-) > > Thanks. > Giovanni Trust the manual: http://dev.mysql.com/doc/refman/5.0/...insert-id.html |