SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recall last entered id from mysql table

    I am finishing off my shopping cart, I am just posting the order details into the orders table, which has an auto_increment id.

    I need this ID so I can enter the products into the order_item table.

    Some people have mentioned mysql_num_rows($query) but i am not sure on the syntax for implementing this.

    Thanks

  2. #2
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id should do the trick.
    No, I REALLY dislike having to use Joomla.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recall last entered id from mysql table

    I am finishing off my shopping cart, I am just posting the order details into the orders table, which has an auto_increment id.

    I need this ID so I can enter the products into the order_item table.

    Some people have mentioned mysql_num_rows($query) but i am not sure on the syntax for implementing this.

    Thanks

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    mysql_insert_id()
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gap_tooth_clan View Post
    Some people have mentioned mysql_num_rows($query) but i am not sure on the syntax for implementing this.
    The above is a hideous practice and should be ignored at all cost

    As crowden siad, use mysql_insert_id directly after the insert query to retrieve the primary key for the last row which was inserted.

    Alternatively, you could use:
    Code:
    SELECT MAX(primaryKey) FROM sometable

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    mysql_insert_id() is what you might be looking for - it returns the last rows' id number where the row is a properly formed auto-increment primary key.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    threads merged

    see how much unnecessary activity was caused by duplicate posts? please don't do that

    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
  •