Php mysql transaction rollback

with regard to mysql transaction rollback, how to roll back if multiple statements are involved?
for example:

sql
Jun.19,2022

try
{
    begin
    
    create order
    
    create order item
    create order item
    
    update stock
    update stock
    
    commit
}
catch(\Exception $ex)
{
    rollback
}

whether you use native php or framework, just follow the above syntax structure


if the same database, according to the above structure, if sub-library, it may be necessary to use message queue to ensure data consistency.


after you open transaction

$mysqli -> begin_transaction ( MYSQLI_TRANS_START_READ_ONLY );

$mysqli -> query ( "SELECT XXXX" );
$mysqli -> commit ();

$mysqli -> close ();

if all statements at the beginning and end of a successful transaction fail as long as one sentence fails, the whole transaction fails if $mysqli-> commit (); is false

$mysqli->rollback()

as for how you are talking about rollback, because you are dealing with items, if the new order fails, let it all be rolled back.


rollback seems to be one way, commit if all statements succeed, and rollback if one fails. Only innodb can do this. Myisam does not support transactions

Menu