Is it possible to perform mysql database transactions and rollbacks using php?

example: transferring a payment from user A to user B. User account: -10 USD User account B: +10 USD

if there is a transaction, and something went wrong, everything was canceled. Thus, with transactions, it does not happen that the user account of A decreases by 10, and the account of user B does not increase by 10.

I know that java people use transactions and rollbacks all over the place. But I have never heard of PHP guys who do this.

+3
source share
2 answers
$db = new mysqli("localhost", "", "", "");
$db->autocommit(FALSE);
if ($db->query("INSERT ..."))
    $db->commit();
else
    $db->rollback();

Make sure your tables use the engine InnoDB: MyISAMdoes not support transactions.

:

InnoDB , MySQL, - MyISAM.

MySQL InnoDB, , .

MySQL InnoDB .

+5

Source: https://habr.com/ru/post/1709512/


All Articles