roice Posted June 14, 2012 Share Posted June 14, 2012 Hello all, In simple words - what is the different between: LAST_INSERT_ID() and mysql_insert_id() ? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 14, 2012 Share Posted June 14, 2012 One is a MySQL function and the other is a php function. Quote Link to comment Share on other sites More sharing options...
roice Posted June 14, 2012 Author Share Posted June 14, 2012 oh, I didn't thought about it... how LAST_INSERT_ID() works? Quote Link to comment Share on other sites More sharing options...
roice Posted June 14, 2012 Author Share Posted June 14, 2012 when I'm using mysql_insert_id() where can I set which table I want to check? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 14, 2012 Share Posted June 14, 2012 That question indicates you're probably trying to use that function for something it wasn't intended to do. Maybe it would best if you explain what you're trying to achieve. Quote Link to comment Share on other sites More sharing options...
roice Posted June 14, 2012 Author Share Posted June 14, 2012 OK until now I insert data to first table, and than I did select for the last ID in order to put it in another table: // Update DB $query3 = "INSERT INTO `tkts_replies` (tkt_id, open_by, name) VALUES('$tkt_id', '$user_id', '$user_name' )"; $res3 = mysql_query($query3); if (!$res3) die('Invalid query: ' . mysql_error()); else { $query4 = mysql_query("SELECT id FROM `tkts_replies` ORDER BY id DESC LIMIT 0,1"); $index = mysql_fetch_array($query4); $reply_id = $index['id']; But I bump into this new function: mysql_insert_id(); I just want to know if the new function is better for my script... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 14, 2012 Share Posted June 14, 2012 OK, you just need to call mysql_insert_id immediately after the first INSERT query. Then you can eliminate the following SELECT query entirely. <?php // Update DB $query3 = "INSERT INTO `tkts_replies` (tkt_id, open_by, name) VALUES('$tkt_id', '$user_id', '$user_name' )"; $res3 = mysql_query($query3); if (!$res3) die('Invalid query: ' . mysql_error()); else { $reply_id = mysql_insert_id(); Quote Link to comment Share on other sites More sharing options...
roice Posted June 14, 2012 Author Share Posted June 14, 2012 Thanks, What will happened if 2 user will submit or run the INSERT function at the same time? Could var $reply_id get the same number for both of them? Roi. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 16, 2012 Share Posted June 16, 2012 Thanks, What will happened if 2 user will submit or run the INSERT function at the same time? Could var $reply_id get the same number for both of them? Roi. No -- auto-increment is per connection. Quote Link to comment Share on other sites More sharing options...
roice Posted June 17, 2012 Author Share Posted June 17, 2012 Thanks, What will happened if 2 user will submit or run the INSERT function at the same time? Could var $reply_id get the same number for both of them? Roi. No -- auto-increment is per connection. Yes, for INSERT comand but what about mysql_insert_id() comand? what will happened if 2 users will submit together and after the INSERT comand will come the mysql_insert_id()? will they both get the same number? Quote Link to comment Share on other sites More sharing options...
kicken Posted June 17, 2012 Share Posted June 17, 2012 Yes, for INSERT comand but what about mysql_insert_id() comand? what will happened if 2 users will submit together and after the INSERT comand will come the mysql_insert_id()? will they both get the same number? No, the ID number that is generated is stored on a per-connection basis. Each user will get the ID that was generated for their insert, not anyone elses. Quote Link to comment Share on other sites More sharing options...
roice Posted June 17, 2012 Author Share Posted June 17, 2012 thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.