ThinkPHP implements database transaction rollback sample code

ThinkPHP provides transaction support for databases. If you want to use transactions in application logic, you can refer to the following methods:
 
Start transaction:
$User->startTrans();

 

Commit transaction:
$User->commit();
 
Transaction rollback:
$User->rollback();

 

Sample code:
$m =M('User');         // Instantiate the User object

$m ->startTrans();     // Start the transaction in the User model

$result=$m->where($where)->delete();

// Perform relevant business logic operations 
if ( $result ){
     $m ->commit();     // Commit if successful 
} else {
     $m ->rollback();     // If unsuccessful, rollback 
}

Note: The transaction operation method provided by the system must be supported by the database itself. If your database or data table type does not support transactions, then the transaction operation of the system is invalid.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205228&siteId=291194637