Analysis of PDO transactions in PHP and phppdo Transaction Processing

Source: Internet
Author: User

Analysis of PDO transactions in PHP and phppdo Transaction Processing

This article analyzes the PDO transaction processing in PHP. We will share this with you for your reference. The details are as follows:

Transaction processing has four features: atomicity, consistency, independence, and durability.

Not all databases support transaction processing. PDO provides transaction support for databases that can execute transaction processing.

Note the following When configuring transaction processing:

1. Disable Automatic submission of PDO;

$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false);

2. methods required to start a transaction;

$ Pdo-> beginTransaction (); // start a transaction $ pdo-> commit (); // submit the transaction $ pdo-> rollback (); // roll back the transaction

3. Generally, the transaction is run in the try... catch... statement, and the catch code segment is executed when the transaction fails.

<? Phptry {$ pdo-> beginTransaction (); // start a transaction $ row = null; $ row = $ pdo-> exec ("xxx "); // execute the first SQL if (! $ Row) throw new PDOException ('prompt message or execute Action'); // if an exception message or an action is displayed $ row = $ pdo-> exec ("xxx "); // execute the second SQL if (! $ Row) throw new PDOException ('prompt message or execute Action'); $ pdo-> commit ();} catch (PDOException $ e) {$ pdo-> rollback (); // execution failed, transaction rollback exit ($ e-> getMessage ();}?>

If an error occurs in the SQL statement in the transaction, all the SQL statements are not executed. When all SQL statements are correct, they are submitted for execution.

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.