+ Reply to Thread
Results 1 to 3 of 3

Thread: MySQL: Getting a newly created entry's id

  1. #1
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Question MySQL: Getting a newly created entry's id

    Hi
    I don't know if I can do what I'm asking for and if I can, I don't know where to find the info.

    To make it short, I have a script that creates a database entry but needs to refer to it right after. But I cannot know the info it will insert in advance. How can I get that entry's id?

    I could simply get the latest id, but I am afraid that the script could be used by two or more people at once and there would be a confusion of the correct entry to use.

    Since PHP is server side, is it sufficient to assume that the latest id/entry will be the one created by this very user at this very moment (100% sure) or not? And if not, is there another way to know which entry was just created (since none of the info inserted is unique)?
    I used to create a customized secondary id that could be found right away using the variables used to create it, but it's rather inefficient resource-wise considering that the primary id key is already unique and much shorter thanks to the auto increment.

    Thanks in advance for the advice!

  2. #2
    TheMan177 is offline x10 Sophmore TheMan177 is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    179

    Re: MySQL: Getting a newly created entry's id

    If I understand your question correctly, I don't think you'll have a problem. This operates using the script's last connection, and even if you have multiple users at once, each instance of that script should have its own seperate link to MySQL.

    MySQL's manual states:

    Quote Originally Posted by http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
    For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

  3. #3
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: MySQL: Getting a newly created entry's id

    Thanks a lot!
    To be honest I didn't even know about LAST_INSERT_ID() (still a beginner) so I'll have a look at it and according to your quote it's gonna be exactly what I should have done from the start.

    Added to your rep!

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. Newly Created Account Problems
    By jeffgriffin in forum Free Hosting
    Replies: 2
    Last Post: 05-22-2008, 06:28 PM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers