SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with somrthing slightly SQL related?

    I'm working on a forum, and i need to create an insert in for one table which will generate an "auto increment" id key, then use that key for the next query, how can i accomplish this in the easiest way possible ?? at the moment I'm using the following:
    PHP Code:
    $insert_thread mysql_query("INSERT INTO threads (id,starter,forum_id,title) VALUES ('','" $user['id'] . "','" $_POST['forumid'] . "','" .  $_POST['f_title'] ."')");
    $this_id mysql_fetch_array(mysql_query("SELECT id FROM threads ORDER BY id DESC"));
    $insert_post mysql_query("INSERT INTO posts (id,content,author,thread_id,title) VALUES ('','" $_POST['f_content'] . "','" $user['id'] . "','" $this_id['id'] . "','" $_POST['f_title'] . "')"); 
    (the middle line is to get the newest ID)

    any better solutions ?

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this is any help...

    SQL Server 7 has: 'select @@identity' which returns the last id created.

    SQL Server 2000 has : 'select IDENT_CURRENT(TableName)'

    Whether mysql has either of those I don't know.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get the last insert id for mysql using mysql_insert_id

    See this:

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

    best regards
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  4. #4
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks both of you. I think the mysql_insert_id() function thingy ought to do the trick


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
  •