Mysql transaction processing application instance of pdo in php

Source: Internet
Author: User
Php + mysql transaction processing steps: 1. disable automatic submission 2. start transaction processing 3. if an exception exists, the system automatically throws an exception and prompts you to roll back. 4. note: Only the InnoDB driver in mysql supports transaction processing. the default MyI... several steps for php + mysql transaction processing:

1. disable automatic commit. 2. enable transaction processing. 3. if an exception exists, an exception is automatically thrown, prompting you to roll back. 4. enable automatic commit.

Note: Only the InnoDB driver in mysql supports transaction processing. the MyIsAM driver does not support transaction processing by default. The following is the instance code:

 0); // The last step is to disable automatic submission. // $ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 0 ); // This is done by setting the attribute method to disable automatic submission. the preceding function is the same as $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION ); // enable exception handling} catch (PDOException $ e) {echo "database connection failed :". $ e-> getMessage (); exit; // open-source code phprm.com}/** transaction processing ** Zhang San bought a 2000 yuan computer from Li Si * deducted 2000 yuan from Zhang San's account * added 2000 yuan to Li Si's account * remove a computer from the product table * MyIsAM InnoDB */try {$ pdo-> beginTransaction (); // enable transaction processing $ price = 500; $ s Ql = "update zhanghao set price = price-{$ price} where id = 1"; $ affected_rows = $ pdo-> exec ($ SQL); if (! $ Affected_rows) throw new PDOException ("Michael Jacob transfer failed "); // The error throws an exception $ SQL = "update zhanghao set price = price + {$ price} where id = 3"; $ affected_rows = $ pdo-> exec ($ SQL ); if (! $ Affected_rows) throw new PDOException ("failed to transfer to Li Si"); echo "transaction successful! "; $ Pdo-> commit (); // commit upon successful transaction} catch (PDOException $ e) {echo $ e-> getMessage (); $ pdo-> rollback () ;}$ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 1); // automatic submission. if not, transfer failed // Set error report mode ERRMODE_SILENT ERRMODE_WARNING


Address:

Reprinted at will, but please attach the article address :-)

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.