We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

mysql_insert_id() or LAST_INSERT_ID()

amitbravo
amitbravo asked
on
Medium Priority
387 Views
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 ?
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
that code will work fine, as you use the same connection/session.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
UmeshSenior Principal Technical Support Engineer
Top Expert 2009

Commented:
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')");
UmeshSenior Principal Technical Support Engineer
Top Expert 2009

Commented:
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 ..

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

Author

Commented:
then I am going to use mysql_insert_id() .
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.