Welcome to WebmasterWorld Guest from 54.196.244.45

Forum Moderators: open

Message Too Old, No Replies

Which row did I just insert?

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

Full Member

5+ Year Member

joined:Mar 22, 2008
posts: 315
votes: 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?
1:32 am on June 14, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Mar 22, 2008
posts: 315
votes: 0


NM.

[w3schools.com...]
1:02 pm on June 14, 2011 (gmt 0)

Administrator

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

joined:July 31, 2003
posts:12533
votes: 0


Yep, LAST_INSERT_ID().
[dev.mysql.com...]
4:57 pm on June 14, 2011 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members