SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get mysql data from insert

    I am inserting some data into a mysql table which has an auto_increment id column.

    Is there a way to get the number from that column so I can use it in the same script?

    i.e. the INSERT function is run and then I can assign the auto_increment number to a variable?

    PHP Code:
    // Insert data
      
    $sql mysql_query("INSERT INTO table SET name='$name'");
      
      
    // do something to get the auto_increment number that was created in table for the INSERT
      
      
    $name_id =  // <- assign it to a variable 

  2. #2
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you want to use the function mysql_insert_id($sql)) to retrieve the last id of the insert query

  3. #3
    SitePoint Addict phpster's Avatar
    Join Date
    Feb 2005
    Location
    Toronto, Canada
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $name_id = mysql_inset_id($result);
    phpster

    I wish my computer would do what I want it to.
    Not what I tell it to do...

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool thanks

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmm

    When I try it I get

    PHP Code:
    Warningmysql_insert_id(): supplied argument is not a valid MySQL-Link resource in /home/.... 
    PHP Code:
    // Insert data
       
    $sql mysql_query("INSERT INTO table SET name='$name'");
       
       
    $name_id mysql_insert_id($sql); 
     
       echo 
    $name_id

  6. #6
    SitePoint Enthusiast Bobbi's Avatar
    Join Date
    Mar 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I ran into issues once with both of them ... so I ended up doing it with a query like this:
    PHP Code:
    $lastid mysql_result(mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 0,1"),0,"id"); 
    Not the cleanest solution, but at least it works

    Bobbi

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by direwolf
    hmmmm

    When I try it I get

    PHP Code:
    Warningmysql_insert_id(): supplied argument is not a valid MySQL-Link resource in /home/.... 
    PHP Code:
    // Insert data
       
    $sql mysql_query("INSERT INTO table SET name='$name'");
       
       
    $name_id mysql_insert_id($sql); 
     
       echo 
    $name_id
    mysql_insert_id() requires connection id or just nothing as param, not the query result.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh yes!

    Thanks for that. Removed the $sql out and now it works a treat.


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
  •