Professional Web Applications Themes

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

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

  1. #1

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

    Giovanni Guest

  2. #2

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

    On 5 Jun, 14:21, "Giovanni R." <it> wrote: 

    The second user will be running with a different connection. They will
    get their last insert id, you will get yours.

    Captain Guest

  3. #3

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

    Captain Paralytic wrote:
     

    Ok, I'll trust you. :-)

    Thanks.
    Giovanni

    Giovanni Guest

  4. #4

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

    On 5 Jun, 15:35, "Giovanni R." <it> wrote: 
    >
    > Ok, I'll trust you. :-)
    >
    > Thanks.
    > Giovanni[/ref]

    Trust the manual:
    http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html

    Captain Guest

Similar Threads

  1. Queries Of Queries Single Quote Problem
    By TimH2O in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 1st, 07:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139