What if two or more users run the queries at the same moment?

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 ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007
Giovanni R.
 
Posts: n/a
Default What if two or more users run the queries at the same moment?


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().

Reply With Quote
  #2 (permalink)  
Old 06-05-2007
Captain Paralytic
 
Posts: n/a
Default Re: What if two or more users run the queries at the same moment?

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.

Reply With Quote
  #3 (permalink)  
Old 06-05-2007
Giovanni R.
 
Posts: n/a
Default Re: What if two or more users run the queries at the same moment?

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

Reply With Quote
  #4 (permalink)  
Old 06-05-2007
Captain Paralytic
 
Posts: n/a
Default Re: What if two or more users run the queries at the same moment?

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:52 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0