PHP transaction control for MySQL operations, rollback

Source: Internet
Author: User
Tags savepoint
<?php//transaction control, rollback//Create a Mysqli object $mysqli = new mysqli ("hostname", "MySQL User name", "Password", "database Name");//Determine if the link succeeds if ($mysqli Connect_error) {die ($mysqli->connect_error);} Due to the system default commit in the transaction commit, it is set to false first not commit $mysqli->autocommit (false);//Actually here the system is quite here to make a savepoint, record all the state at this time, rollback is rollback to here//$mysqli-  >savepoint a;//writes the operation statement to the database $SQL1 = "INSERT into table name (field name) values (corresponding field value)"; $sql 2 = "Update table name set field name = value where Condition"; $sql 3 = Delete from table name where condition;//Execute SQL statement $res1 = $mysqli->query ($sql 1) or Die ($mysqli->error); $res 2 = $mysqli->query ( $sql 2) or Die ($mysqli->error), $res 3 = $mysqli->query ($sql 3) or Die ($mysqli->error);//Determine if all success is performed if (! $res 1| |! $res 2| |! $res 3) {//As long as there is a failure to rollback, do not execute, if you set the rollback point, such as a, add a parameter A to roll back to a where $mysqli->rollback ();} else{//once the commit cannot be rolled back, the $mysqli->commit () is submitted successfully;} Close resource $musqli->close ();? >

Related Article

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.