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
https://forums.phpfreaks.com/topic/63212-solved-retrieve-newly-inserted-record/
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();

 

 

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.