[Last Call] Learn how to a build a cloud-first strategyRegister Now


mysql_insert_id() or LAST_INSERT_ID()

Posted on 2009-02-14
Medium Priority
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
  • 2
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 23640886
that code will work fine, as you use the same connection/session.
LVL 26

Expert Comment

ID: 23640888
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

ID: 23640914
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

ID: 31546904
then I am going to use mysql_insert_id() .

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 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