1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Insert Rows into MySQL and immediately retrieve row id

Discussion in 'PHP' started by adamjthompson, Nov 21, 2008.

  1. #1
    I want to insert a new row with data into a MySql table, then be able know what the row id is (row id is an autoincrement column in mysql, so the value is sent by mysql).

    Let's say (for example) that this is my table structure:

    CREATE TABLE IF NOT EXISTS `subpages` (
      `subpage_id` mediumint(3) NOT NULL auto_increment,
      `meta_title` varchar(120) NOT NULL default '',
      KEY `subpage_id` (`subpage_id`)
    ) ;
    Code (markup):
    I use PHP to insert a row of data. I then want to use PHP to reference the newly inserted row by subpage_id. How do I know what the correct value is?

    Hope this makes sense...if anyone can point me to a tutorial or info for this, that would be great. Thanks!
     
    adamjthompson, Nov 21, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    juust, Nov 21, 2008 IP
    adamjthompson likes this.
  3. adamjthompson

    adamjthompson Well-Known Member

    Messages:
    1,242
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    125
    #3
    OK, so I found a thread on this issue over here.

    According to that thread, this is the code I need:

    $lastID = mysql_insert_id($link);
    Code (markup):
    But...what is the $link variable? Do I need to set that? Or just leave it empty?
     
    adamjthompson, Nov 21, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    How are you connecting to mysql and querying? plain mysql, mysqli, etc...

    Generally:

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

    mysql_select_db('db');

    $query = mysql_query("INSERT.....");

    $last_id = mysql_insert_id();

    mysqli works a little differently.
     
    jestep, Nov 21, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    As long as you don't run another insert query on the actual page that the script is running on, it will retrieve the proper id. The insert_id is attached to the link / query that you just completed and not the database itself, so there isn't a chance of a collision or missed id.
     
    jestep, Nov 21, 2008 IP
  6. adamjthompson

    adamjthompson Well-Known Member

    Messages:
    1,242
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Not sure. I'm guessing plain MySQL. Here is my connection code:

    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    
    mysql_select_db($db) or die ("Unable to select database!");
    Code (markup):
     
    adamjthompson, Nov 21, 2008 IP
  7. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you don't use a link-id for your database connection and query, you can ignore the $link bit.

    If you work with connections to different databases (or hosts) you would use a link id for the different connections.

    http://nl2.php.net/manual/en/function.mysql-connect.php
     
    juust, Nov 21, 2008 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    The code above should work fine then. Just put the insert_id function after the insert statement.
     
    jestep, Nov 21, 2008 IP
    adamjthompson likes this.
  9. adamjthompson

    adamjthompson Well-Known Member

    Messages:
    1,242
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    125
    #9
    Yup, it works. Thanks, mate!
     
    adamjthompson, Nov 21, 2008 IP