Transaction processing Flow
- Open transaction
Pdo::begintransaction
- Transactional operations
All the practical operation is to delete and change
- Transaction commit
Pdo::commit: Submit data after success
Pdo::rollback: Rollback data after failure
<?PHP//PDO Transaction processing//connection authentication $pdo=NewPDO (' Mysql:host=localhost;dbname=project ', ' root ', ' root '); //1. Turn on the transaction $res=$pdo-BeginTransaction (); //Var_dump ($res); 2. Transaction processing (multiple SQL statement executions) $sql= "Update pro_student set s_age = s_id = 20"; $lines=$pdo-exec($sql); //Var_dump ($lines); $sql= "SELECT * from pro_student where s_id = 20"; $stmt=$pdo->query ($sql); //Var_dump ($stmt->fetch (PDO::FETCH_ASSOC)); 3. Commit a transaction if($lines){ //Update Successful $pdo-commit (); }Else{ //Update failed EchoFailed; $pdo-rollback (); }
Attention
- To implement a transaction, the storage engine for the data table must be InnoDB
- Transaction must be complete (open à submit)
PHP--PDO transaction processing