My transaction rollback

Source: Internet
Author: User

1<?PHP2     Header("Content-type:text/html;charset=utf-8");3     require_once"SqlHelper.class.php";4 5     $sql 1= "Update count set balance=balance-2 where id=1";6     $sql 2= "Update count set balance=balance+2 where id=2";7     $mysqli=NewSqlHelper ();8     //autocommit (false) 9 $mysqli->commit (false);Ten     $result 1=$mysqli-&GT;EXECUTE_DML ($sql 1); One     $result 2=$mysqli-&GT;EXECUTE_DML ($sql 2); A     if(!$result 1|| !$result 2) { -         #code ... -         echo "failure, rollback"; $mysqli->commit (back); -}Else{ -         echo "Success"; $mysqli->commit (ture); +     } -  +?>

Tool class

1<?PHP2 3     /**4 * Function Name: sqlHelper.class.php5 * Function Function: mysqli object-oriented dql DML query6 * Function Zhang Zhengui7 * Creation time: 2015-01-058 * Modified time:9     */Ten     classSqlHelper One     { A         Private $mysqli; -         Private Static $host= ' localhost '; -         Private Static $root= "Root"; the         Private Static $password= ""; -         Private Static $dbname= "Test"; -          Public function__construct () -         { +             #code ... -             $this->mysqli =NewMysqli (self::$host, Self::$root, Self::$password, Self::$dbname); +             if($this->mysqli->connect_error) { A                 #code ... at                  die("Connection failed.")$this->mysqli->connect_error); -             } -             $this->mysqli->query ("Set Names UTF8"); -  -         } -          Public functionEXECUTE_DQL ($sql){ in             $res=$this->mysqli->query ($sql) or die("Dql failed".)$this->mysqli->error); -             return $res; to         } +          Public functionEXECUTE_DML ($sql){ -             $res=$this->mysqli->query ($sql) or die("DML failed".$this->mysqli->error); the             if(!$res) { *                 #code ... Failed $                 return0;Panax Notoginseng}ElseIf($this->mysqli->fetch_rows > 0) { -                 #code ... Success the                 return1; +}Else{ A                 #code ... Does not affect the number of rows the                 return2; +             } -         } $        Public function Commit ($b) {$b ==false) {# code...//transaction rollback $this- >mysqli->autocommit (false);}elseif ($b ==true) {# code ... Wuyi $this->mysqli->commit ();}elseif ($b ==back) {# # code ... Wu $this->mysqli->rollback ();}56} $     } -?>

My transaction rollback

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.