Php+mysql how to implement transaction rollback, is the table MyISAM type?

Source: Internet
Author: User
Keywords Php
MyISAM does not support transactions, but how do I implement a rollback if I encounter a database error? for example:
A program must first insert a table, and then insert the B table. The following line is not logical:

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

Reply content:

MyISAM does not support transactions, but how do I implement a rollback if I encounter a database error? for example:
A program must first insert a table, and then insert the B table. The following line is not logical:

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

Change into InnoDB

Binlog

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

    1. Either the storage engine is changed InnoDB ;

    2. Either save the statement for each execution SQL ;

    3. or a regular part.

Upstairs said binlog I am also very curious what the ghost, my answer above narrow. So I found this thing: mysqlbinlog+myisam implementation of MySQL anytime, anywhere recovery method

MyISAM does not support transactions, change to InnoDB bar
and Binlog can recover data, but not real-time recovery.

Table with InnoDB Bar.
Then define an exception, such as that the table does not exist
Begin
DECLARE flag varchar (32);
--Defining exceptions
Declare exit handler for 1146
Begin

rollback ;...

End
Set autocommit = 0; --Open transaction
Aresut=doinserta

if (Aresut) {
Bresult=doinsertb

if (!bResult){    -- 触发异常回滚    

}
--Submit
commit;

There is no way, MyISAM is not support business, change to InnoDB bar

If you do not need to use MyISAM, you can use this to insert the temporary table first, if the execution succeeds insert tableA from select id name from tableb . This simulates things. 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.