SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Question][Mysql] I want to know... [Mysql][Question]

    I want to know if we can insert data in several table with the same ID???????

    Thanks!

    -Amosjb

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    the answer is yes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I don't really understand ... For example, someone register on my site (it will create a row in table 'user') but some information will be entered in table 'user_info', but there are also table that will be automatically opens like the table 'money'. The user who have an id 1 in 'user' must have in table 'user_info' row with id 1 and also in table 'money'. But if many people are register in the same time, what could happen????? the information will be mixed???

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    what should happen is that your application code will use the LAST_INSERT_ID function

    check da manual --
    The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This means that if AUTO_INCREMENT, to a client, gave the ID 132 in the table 'user', AUTO_INCREMENT will give the same ID in the two other tables('user_info' and 'money')??

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    no, you would retrieve the appropriate value using LAST_INSERT_ID -- the other tables should not have an auto_increment column
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you do an example with that code and LAST_INSERT_ID please...

    $dnn3 = $bdd->prepare('INSERT INTO user(firstname, lastname, username, password, email, regdate) VALUES (?, ?, ?, ?, ?, NOW())');
    $dnn3->execute(array($firstname, $lastname, $username, md5($password), $email));
    $dnn3->closeCursor();
    $dnn4 = $bdd->prepare('INSERT INTO user_info(birthdate) VALUES (?)');
    $dnn4->execute(array($birthday));
    $dnn4->closeCursor();

    Where I should enter the LAST_INSERT_ID???

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    sorry, man, i don't do perl or whatever that is
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It php.... You don't know php???

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    that is correct sir, i don't do php either
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See: http://php.net/manual/en/function.mysql-insert-id.php

    You have to execute that function after you have executed your insert query.


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
  •