Jump to content

[SOLVED] Retrieve newly inserted record


10legit10quit

Recommended Posts

Have a quick question about PHP & SQL: is there any way to find info of a newly inserted record? For example a record is being inserted with no value for a timestamp field, so it is filled automatically when the record is inserted. Is there any way to quickly retrieve this entry information?

 

I'm not sure if "Select last_insert_id" will work properly, because with multiple users it is possible that anther record has been inserted already.

 

Thanks

 

Link to comment
Share on other sites

Unless I'm mistaken, if the users are on different connections to the DB then last_insert_id will return the ID for the user calling it and not other users.

The database is being accessed through a PHP file on the webserver, so it might be seen as just one user.

 

Actually "mysql_insert_id();" might be the correct command to use, here is the current code. It seems to work ok but I am worried about problems with multiple users.

$sql = 'INSERT INTO `TransactionHistory` (`thRef`, `thDate`, `thSenderAccount`, `thRecipientAccount`, `thSenderKey`, `thRecipientKey`, `thBalance`, `thLabel`, `thThreadNumber`, `thEnteredByForumID`, `thMemo`, `thPMemo1`, `thPMemo2`) VALUES (NULL, "'
.$transDate
			.'", "'
.$sendAccName
			.'", "'
.$recAccName
			.'", "'
.$sendAccKey
			.'", "'
.$recAccKey
			.'", "'
.$transAmt
			.'", "'
.$transLabel
			.'", "'
.$transThreadNum
			.'", "'
.$enteredByID
			.'", "'
.$transMemo
			.'", "'
.$transPMemo1
			.'", "'
.$transPMemo2
			.'")';

			//perform query
			$result = mysql_query($sql);
//get recent id
			$lastid = mysql_insert_id();

 

 

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.