SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get current ID when inserting in mySQL

    I created a table with columns ID, Message and Author. ID is int, primary key, unique for each entry. Now when I add new entry into database i would like to know what ID was assigned to it. How can that be done?

    Using SELECT ID from.... WHERE Author LIKE .... AND Message LIKE ...
    is not OK because there can be 2 entries with the same content, so I would get wrong ID.

    Any ideas ?

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id() will do the job!


    mysql_insert_id*--* Get the id generated from the previous INSERT operation


    http://www.php.net/manual/en/functio...-insert-id.php

    I use it like this after a submit:
    $new_id_number = mysql_insert_id();

    tada!

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it worked

    thanx

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure... glad it worked!


Bookmarks

Posting Permissions

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