How does php + mysql implement transaction rollback, and tables are of the MyISAM type?

Source: Internet
Author: User
MyISAM does not support transactions, but how can I roll back a database error? For example, a program needs to insert Table a before table B. The logic is as follows: {code...} MyISAM does not support transactions, but how can I implement rollback if a database error occurs? For example:
A program needs to insert Table a before inserting table B. The logic is as follows:

aResut=doInsertAif(aResut){   bResult=doInsertB    if (!bResult){        delete aResult from a    } }

Reply content:

MyISAM does not support transactions, but how can I roll back a database error? For example:
A program needs to insert Table a before inserting table B. The logic is as follows:

aResut=doInsertAif(aResut){   bResult=doInsertB    if (!bResult){        delete aResult from a    } }

Change to innodb

Binlog

Hi, you're right.MyISAMRollback is not supported. However, there are several measures to prevent data loss in advance:

  1. Either change the storage engineInnoDB;

  2. EitherSQLStatement to save;

  3. Or regular backup

Said upstairs.binlogI'm also curious about what it is. My answer is too narrow. So I found this: mysqlbinlog + MyISAM Implementation of mysql restoration anytime, anywhere

Myisam does not support transactions. Change it to innodb.
In addition, binlog can recover data, but cannot be recovered in real time.

Use innodb for tables.
Define an exception. For example, the table does not exist.
Begin
Declare flag varchar (32 );
-- Definition exception
Declare exit handler for 1146
Begin

rollback ;...

End
Set autocommit = 0; -- start the transaction
AResut = doInsertA

If (aResut ){
BResult = doInsertB

If (! BResult) {-- trigger an exception rollback insert into abcdefghijklmn (a) values ('A ');}

}
-- Submit
Commit;

No way. myisam does not support transactions. Replace it with innodb.

If you do not need to use myisam, you can use this method to insert a temporary table first. If the execution is successful, useinsert tableA from select id name from tablebSimulate things in this way. Can be encapsulated as a stored procedure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.