SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting the row back from an insert query...?

    Is there any way to get MySQL to retrieve a newly inserted row?

    It seems my code is doing this sort of thing a lot:

    insert into users_table(a, b, c, d) values ("a", "b", "c", "d");
    var x = select from user_id from users_table where a="a";
    update other_table set user_id = "x" where something

    In other words, inserting a new row and then immediately having to retrieve the key for that row in order to link it in some other table.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if the table you are inserting into has an AUTO_INCREMENT column (you didn't mention), then you can use the LAST_INSERT_ID function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •