SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Big Fat JC's Avatar
    Join Date
    Jun 2001
    Location
    Saskatchewan, Canada
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get ID in PHP & MySQL

    Hi,

    This is a pretty elementary question I think, but anyway I've setup my database and all and I have this PHP program that adds a record to the database. The ID column is set to auto_increment so that it keeps going up which is great, but once I add the record i'd like to know what the ID is so that I can use that number. Is there a way to do this?

    Thanks for all the help.
    Eden Rox
    Project Bookie Currently Under Development

  2. #2
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use a sql statement similar to the following:


    SELECT id FROM table_name ORDER DESC;

    then the first record that comes up will be the value you need.

  3. #3
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id()

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

    mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed.
    Example:

    PHP Code:
    mysql_query("INSERT INTO widgets (name) VALUES ('widget1')");
    echo 
    "ID number of inserted widget: ".mysql_insert_id(); 

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by compwizard
    use a sql statement similar to the following:


    SELECT id FROM table_name ORDER DESC;

    then the first record that comes up will be the value you need.
    do not use that! another client could INSERT a row before you run that query, and you'd get the wrong id. as cyngon showed, mysql_insert_id() is made just for this and is faster.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point DR_LaRRY_PEpPeR. I am pretty new to php but am seasoned at sql so I was thinking in terms of only using sql statements, but again your method is much better.


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
  •