Mysql transaction processing usage instance of pdo in php, pdomysql_PHP tutorial

Source: Internet
Author: User
Mysql transaction processing usage instance of pdo in php, pdomysql. Mysql transaction processing usage example of pdo in php. pdomysql this article describes the mysql transaction processing usage of pdo in php. Share it with you for your reference. The specific analysis is as follows: mysql transaction processing usage instance of pdo under php + mysql transaction php, pdomysql

This article describes the usage of mysql transaction processing for pdo in php. Share it with you for your reference. The specific analysis is as follows:

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:

The code is as follows:

<? Php
Try {
$ Pdo = new pdo ("mysql: host = localhost; dbname = mydb", "root", "root", array (PDO: ATTR_AUTOCOMMIT => 0 )); // disable automatic submission.
// $ Pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 0); // This is to disable automatic submission by setting the attribute method, which is the same as the above function
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // enable exception handling
} Catch (PDOException $ e ){
Echo "database connection failed:". $ e-> getMessage ();
Exit;
}
/*
* Transaction processing
*
* John bought a 2000 yuan computer from Li Si.
* Deduct RMB 2000 from Michael Jacob's account
* Add RMB 2000 to the account Li Si
* Reduce a computer from the commodity table
* MyIsAM InnoDB
*/
Try {
$ Pdo-> beginTransaction (); // start transaction processing
$ Price = 500;
$ SQL = "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 (); // submit if the transaction is successful
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
$ Pdo-> rollback ();
}
$ Pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 1); // automatic submission. if not, the transfer is unsuccessful.
// Set the error report mode ERRMODE_SILENT ERRMODE_WARNING
?>

I hope this article will help you with php programming.

Examples in this article describes the usage of mysql transaction processing for pdo in php. Share it with you for your reference. The specific analysis is as follows: php + mysql transaction...

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.