mysql_insert_id() or LAST_INSERT_ID()

Posted on 2009-02-14
Last Modified: 2013-12-12
I need to do like >
$sql=mysql_query("INSERT INTO blog (id, subject) VALUES (NULL,'mytext')");
just after this I need to insert another information in another table where I need to set same Unique ID of last query like :

$sql2=mysql_query("INSERT INTO blog2 (id, subject) VALUES (mysql_insert_id(),'second text')");

Though this all works fine but I am wondering what if 1000 users are visiting the site same time and inserting rows so could it be incorrect because of it . what after executing first line from a users PC and before second line  if some other user execute the  same the code ?
Question by:amitbravo
    LVL 142

    Accepted Solution

    that code will work fine, as you use the same connection/session.
    LVL 26

    Expert Comment

    Use LAST_INSERT_ID() instead of mysql_insert_id()

    so your query should be

    $sql2=mysql_query("INSERT INTO blog2 (id, subject) VALUES ( LAST_INSERT_ID(),'second text')");
    LVL 26

    Expert Comment

    sorry I misread the question.. both works LAST_INSERT_ID() & mysql_insert_id().. and there would not be any problem no matter 1000 user or 1000K user ..

    Author Closing Comment

    then I am going to use mysql_insert_id() .

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now