Welcome to WebmasterWorld Guest from 54.196.233.239

Forum Moderators: open

Message Too Old, No Replies

Which row did I just insert?

     

webfoo

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

5+ Year Member



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

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

5+ Year Member



NM.

[w3schools.com...]

coopster

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

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



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

rocknbil

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

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



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month