RNK
RNK

Reputation: 5792

How to go back with a SQL query if an error occurs?

I want to achieve accurate accounting in php and mysql. My code is working 100% fine.

For Debit:

$query = new QUERY(array('TABLE'=>'account_treasury_local_agent'));
$query->save($data);

And for Credit:

$query = new QUERY(array('TABLE'=>'account_treasury_headquarters'));
$query->save($data2);

The problem is, I want to revert whole transaction if there is an error in second function. I mean, I want to run this two queries together. Without debit, credit shouldn't be possible and vice versa.

EDIT:

I know, it's possible by means of TRANSACTION START, COMMIT, ROLLBACK. But, I have no idea, how to implement it with php.

Thanks.

Upvotes: 1

Views: 943

Answers (1)

VoteyDisciple
VoteyDisciple

Reputation: 37803

To begin the transaction: BEGIN (or START TRANSACTION).

To commit: COMMIT.

To roll back: ROLLBACK.

That's all there is to it.

Upvotes: 1

Related Questions