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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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