Jump to content

Last id


roice

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/264163-last-id/#findComment-1353772
Share on other sites

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();

Link to comment
https://forums.phpfreaks.com/topic/264163-last-id/#findComment-1353777
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/264163-last-id/#findComment-1354558
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/264163-last-id/#findComment-1354559
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.