Link to home
Start Free TrialLog in
Avatar of amitbravo
amitbravo

asked on

mysql_insert_id() or LAST_INSERT_ID()

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 ?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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')");
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
Avatar of amitbravo
amitbravo

ASKER

then I am going to use mysql_insert_id() .