homepage Welcome to WebmasterWorld Guest from 54.234.217.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Which row did I just insert?
webfoo




msg:4325738
 1:26 am on Jun 14, 2011 (gmt 0)

I created a PHP file to insert a new record into a database. The table's ID field is set to auto increment. Multiple users may be inserting rows simultaneously, so the highest ID is not a good indicator for which row I just inserted. How can I get the ID from the row I just inserted?

 

webfoo




msg:4325740
 1:32 am on Jun 14, 2011 (gmt 0)

NM.

[w3schools.com...]

coopster




msg:4325900
 1:02 pm on Jun 14, 2011 (gmt 0)

Yep, LAST_INSERT_ID().
[dev.mysql.com...]

rocknbil




msg:4326027
 4:57 pm on Jun 14, 2011 (gmt 0)

Yeah but make sure you get via last insert id immediately after doing the insert . . . I always use a new select with the unique data I just inserted to make sure it's the relevant record.

Select id from table where email='inserted@email.com' and user_id=12345;

It's always a **very good idea** to use a second field for user_id or other unique id rather than the auto increment field.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved