...

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



paddyfields
05-13-2011, 12: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.
Patrick.

Old Pedant
05-13-2011, 12:34 AM
http://dev.mysql.com/doc//refman/5.0/en/information-functions.html#function_last-insert-id

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.

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum