Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: how to get the auto increment integer primary key value of a new record

    I have a DB table that has a auto increment integer primary key, if I insert a new record into it by SQL statement "INSERT INTO...", how can I get the integer primary key value of that newly created record?

  2. #2
    Join Date
    Mar 2004
    Posts
    7
    For this purpose, you can use mysql_insert_id().
    It returns the value generated for an auto_increment column by the previous INSERT or UPDATE statement.
    Full documentation to be found in the mySQL manual that can be downloaded from www.mysql.com.

  3. #3
    Join Date
    Mar 2004
    Location
    Pilani
    Posts
    3

    Thumbs up

    hi

    The answer to your problem is very simple.
    you do one thing whenever you give INSERT statement keep that field blank.

    Code:
     INSERT INTO table (id,data1,data2) VALUES ("",$var1,$var2)
    when you keep it blank mysql qill automatically increase its value in database.

  4. #4
    Join Date
    Mar 2004
    Posts
    2
    Thanks binkie. Another question is there may be more than 1 people inserting new records into this table concurrently, so any more things need to do?

  5. #5
    Join Date
    Mar 2004
    Posts
    7
    mySQL keeps track of more people doing simultaneous inserts. That's why you should NOT use a SELECT MAX(KEY) FROM TABLE because someone else may have inserted a record just after you, and will get the wrong key value.
    mySQL links your particular session to the actions that you have executed. By doing so, mysql_insert_id() will return YOUR inserted key, no matter how many people are inserting at that moment.

Posting Permissions

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