Results 1 to 17 of 17

Thread: retreiving the latest record

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    135

    retreiving the latest record

    Hi
    i m using php and mysql..I am inserting the data into the table and straight after that i want to grab the primary key for use. how do i do that

    Code:
    
    $SQL = "INSERT INTO table (name,addressType,suburb,state,postcode,contactName,number_of_standart_stb,number_of_IQ_stb) VALUES ('".$_POST['name']."','".$_POST['addressType']."','".$_POST['Suburb']."','".$_POST['State']."','".$_POST['Postcode']."','".$_POST['contactName']."','".$_POST['standardSTB']."','".$_POST['iqstb']."')";
    now after inserting this record i want to use the primary key( ID) ...how do i retreive that straight away?

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: retreiving the latest record

    You posted this in the code bank! Please read the forum link carefully before you click it next time.

    Edit:

    Which field are you using as a primary key? If it is name then the field would need to be as unique, if it is ID you need to first insert the id into the data (usually the first piece of data inserted into the database).
    Last edited by Nightwalker83; Jan 25th, 2010 at 12:49 AM. Reason: Adding more
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: retreiving the latest record

    Thread Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: retreiving the latest record

    You need to use the mysql_insert_id function.

    "Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT)."

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: retreiving the latest record

    make sure you're not just inserting into your database before sanitising your user's input; you can use mysql_real_escape_string() to do this.

    Nightwalker: when you're inserting into a database, you don't need to provide the primary key as a field if auto_increment is enabled. the value inserted would have to be null for auto_increment to work, so his query is fine. he wanted to retrieve the ID of the record he just inserted -- ILMV's solution.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    135

    Re: retreiving the latest record

    Thanks guys....i think i have got the solutions ...mysql_insert_id() function should work.....i will try it tomorrow....

    Just by curiosity....say for an example ...i added a record and just after than another user added record...will my query give me my ID or the latest user's ID?

    i hope you understood my question...like what happens when the record insertion takes place in parts of nanosecond.....will it still work perfectly and give me the correct ID?

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: retreiving the latest record

    You would be preforming the query of the second user so no. However, what you might be able do is store your search for use later on.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    135

    Re: retreiving the latest record

    Quote Originally Posted by Nightwalker83 View Post
    You would be preforming the query of the second user so no. However, what you might be do is store your search for use later on.
    i only have to use that ID only once...but my confusion is that by mistake (taking the last inserted data in the database ) can the query return the ID which was inserted by another user

    example

    id =1 is inserted by me and at the same time id =2 was inserted by another user

    when i use the mysql_insert_id function ...will it return ID =1 or ID =2 ?

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: retreiving the latest record

    Well it says on the page provided by ILMV that it returns the result for the previous query, so if you had user 1 enter data then user 2 enter data followed by the ID retrieve function the result would be the id for user 2. However, if you put the ID retrieve function after each insert you get both both user 1 and 2.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: retreiving the latest record

    ... nightwalker, what you're saying is completely incorrect. it would be best that if you don't know how something works, you don't try to explain it.

    mysql_insert_id() works for the last query given by the current client connection, not the last query sent to the database in general.

    The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: retreiving the latest record

    Quote Originally Posted by Nightwalker83 View Post
    Well it says on the page provided by ILMV that it returns the result for the previous query, so if you had user 1 enter data then user 2 enter data followed by the ID retrieve function the result would be the id for user 2. However, if you put the ID retrieve function after each insert you get both both user 1 and 2.
    Try spending two seconds reading the documentation before giving people advice which is totally wrong and misleading.

  12. #12
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: retreiving the latest record

    Hi,
    I had also came across last_insert_id, and what's the difference between these two? so,
    Will mysql_insert_id retrieves the last inserted id as per connections???

  13. #13
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: retreiving the latest record

    Hi,
    If mysql_insert_id works based on client connections means, what will really happen for computers connected in the local network?

  14. #14
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: retreiving the latest record

    client connection does not mean a physical internet connection. it means a given connection from the website to the database. every new browser window you have opened on one page will theoretically have a new connection to the database.

    and, what is the difference between what two? this thread is about the function mysql_insert_id() and nothing else -- and that function is pretty well explained in this thread and also in the PHP documentation.

    you're posting a lot of questions lately that are basically answered by reading through the thread you're posting in. and the threads are all a few weeks old.

  15. #15
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: retreiving the latest record

    I simply asked about this last_insert_id

  16. #16
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: retreiving the latest record

    Quote Originally Posted by kows View Post
    you're posting a lot of questions lately that are basically answered by reading through the thread you're posting in. and the threads are all a few weeks old.
    I am thinking of starting a new thread for this last_insert_id and so i continues in this thread.

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: retreiving the latest record

    Quote Originally Posted by bharanidharanit View Post
    I am thinking of starting a new thread
    Please do so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width