homepage Welcome to WebmasterWorld Guest from 54.237.78.165
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, Moderator: open

Databases Forum

    
Which row did I just insert?
webfoo

5+ Year Member



 
Msg#: 4325736 posted 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

5+ Year Member



 
Msg#: 4325736 posted 1:32 am on Jun 14, 2011 (gmt 0)

NM.

[w3schools.com...]

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4325736 posted 1:02 pm on Jun 14, 2011 (gmt 0)

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

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4325736 posted 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