Example of MySQL transaction usage for PDO under PHP, pdomysql_php tutorial

Source: Internet
Author: User

Example of MySQL transaction usage for PDO under PHP, Pdomysql


This example describes the MySQL transaction usage of the PDO under PHP. Share to everyone for your reference. The specific analysis is as follows:

Php+mysql several steps for transaction processing:

1. Turn off Autocommit 2. Turn on transaction 3. Automatically throws an exception prompt and then rolls back 4. Turn on autocommit

Note: MySQL only this innodb driver is support transaction, the default MyISAM driver is not supported, the following is the instance code:
Copy CodeThe code is as follows: <?php
try{
$pdo =new PDO ("Mysql:host=localhost;dbname=mydb", "root", "root", Array (pdo::attr_autocommit=>0));//finally Turn off autocommit
$pdo->setattribute (pdo::attr_autocommit, 0);//This is done by setting the property method to turn off autocommit as well as above function
$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);//Turn on exception handling
}catch (Pdoexception $e) {
echo "Database connection failed:". $e->getmessage ();
Exit
}
/*
* Transaction Processing
*
* Zhang San bought a 2000-dollar computer from John Doe.
* 2000 Yuan deducted from Zhang San account
* Add $2000 to Li four account
* Reduce a computer from the product list
* MyIsAM InnoDB
*/
try{
$pdo->begintransaction ();//Turn on 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 ("Zhang Three turns out 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 ("Transfer to John Doe failed");
echo "Trading Success! ";
$pdo->commit ();//The transaction was successfully submitted
}catch (Pdoexception $e) {
echo $e->getmessage ();
$pdo->rollback ();
}
$pdo->setattribute (Pdo::attr_autocommit, 1);//automatic submission, if not automatically submitted, the transfer is unsuccessful
Set Error Reporting mode Errmode_silent errmode_warning
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/933597.html www.bkjia.com true http://www.bkjia.com/PHPjc/933597.html techarticle example of MySQL transaction processing usage for PDO under PHP, Pdomysql This example describes the MySQL transaction usage of PDO under PHP. Share to everyone 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.