Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    338
    Thanks
    63
    Thanked 11 Times in 11 Posts

    storing auto increment value as it is inserted?

    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.
    Last edited by paddyfields; 05-13-2011 at 12:17 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    http://dev.mysql.com/doc//refman/5.0...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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    OracleGuy

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    338
    Thanks
    63
    Thanked 11 Times in 11 Posts
    perfect, thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •