View Full Version : storing auto increment value as it is inserted?

05-13-2011, 01:15 AM
Hi there.

Is it possible to store the value of an auto_increment value as a PHP variable (in my case event_id) as a record is created using the INSERT method?

What i'm trying to do is let the user insert at event, and then immediately display - 'view your event here example.com/event.php?id=[the event id] ' .

All I can think of is doing a SELECT event_id where eventname = whatever, but there must be an easier way?

many thanks.

Old Pedant
05-13-2011, 01:34 AM

After doing the INSERT, simply then do SELECT last_insert_id() *ON THE SAME CONNECTION*. Don't close or otherwise break the connection and you'll get the right value.

05-13-2011, 01:34 AM
See: http://us2.php.net/manual/en/function.mysql-insert-id.php

05-13-2011, 11:17 AM
perfect, thank you.