Is it possible to retrieve values set by an Update statement
Posted: Thu Aug 20, 2009 1:33 pm
Basically all I am trying to do is add 1 to a field and then retrieve what the new value is... but multiple programs (not written by me) are using the same database so I am worried about race conditions.
- - - - - - - -
Is it possible to retrieve values set by an Update statement?
For example...
Is it possible to know what inventoryNumber is without running a Select statement after the Update statement? I am worried about race conditions since several users may update inventoryNumber in between the time I run the Update statement and the Select statements.
- - - - - - - -
I did write some code that I believe will work and prevent race conditions, but there probably is a much simpler way to go about this.
$success = false;
- - - - - - - -
Is it possible to retrieve values set by an Update statement?
For example...
Code: Select all
$query = "UPDATE inventory_main SET inventoryNumber = inventoryNumber + 1 WHERE inventoryId = 7";
$dbResults = $db->query($query);- - - - - - - -
I did write some code that I believe will work and prevent race conditions, but there probably is a much simpler way to go about this.
$success = false;
Code: Select all
while(!$success){
$query = "SELECT inventoryNumber FROM inventory_main WHERE inventoryId = 7";
$dbResults = $db->query($query);
$row = $dbResults->fetch_assoc();
echo "Inventory Number: ".$row['inventoryNumber']."<br />";
$newFieldValue = $row['inventoryNumber'] + 1;
echo "Attempt: ";
$query = "UPDATE inventory_main SET inventoryNumber = IF (inventoryNumber = ".$row['inventoryNumber'].", inventoryNumber + 1, inventoryNumber) WHERE inventoryId = 7 LIMIT 1";
$dbResults = $db->query($query);
if($db->affected_rows == 1) $success = true;
}