I am about to create a row in a table. This will generate a orderID
with Auto_increment
. Next I want this number (orderID
) pulled out, and I need this number to be 100% from the newly constructed row. My suggestion is the following code.
Will this be safe even if 1.000.000 users run the same routine at same time?
$this->conn->beginTransaction();
try
{
// Create User Table
$stmt = $this->conn->prepare("INSERT INTO moneytransactionstbl(trnsUserID)
VALUES(:trnsUser_ID)");
$stmt->bindparam(":trnsUser_ID",$userID);
$stmt->execute();
// Get the Order ID
$trnsOrderID = $this->conn->lastInsertId();
// Commit this rutine run
$this->conn->commit();
return $trnsOrderID;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
$this->conn->rollBack();
}