1, Pre-order
In order to reconstruct part of the Code of the service-side interface of the app (social class), so it touches the word innoDB, as well as the transaction, the following is mainly in the form of examples to show its usage, the theory of things not too much to tell.
2, what is a transaction
I explained in the programming aspect: many command operations put together processing, for example commits the time to commit together, the revocation time is also the revocation, the book to this word definition content many.
3,myisam and InnoDB
They are all MySQL database engine, we generally build table default is MyISAM engine, compare: http://www.cnblogs.com/vicenteforever/articles/1613119.html, personal think, class Like INSERT, UPDATE, delete if the operation involves multi-table or single-table interconnection, to avoid data write dirty, use transactions. because if an error occurs throughout the process, you can roll back to the start state.
4, share a PHP-based class
1 <?Php23/**4* Created by Phpstorm.5* User: Lin Kuo-hong6* DATE:2016/4/287* TIME:10:208*/9Include "config.php";/** Database configuration information class, self-improvement*/1011Classsql{12Public$link =Null;13Private$config =Null;14/**15* Whether to open the transaction directly16*/17Publicfunction Sql ($begin =False){18$this->config =NewConfig ();19$thisConnect ();20mysql_query ("SET autocommit=0",$this->link);/** Set not auto commit, default is auto commit*/21stIf$begin){22$thisSwbegin ();23}24}2526PublicfunctionConnect () {27$this->link =Mysql_connect ($this->config->host,$this->config->user,$this->CONFIG->PW);/** Connect to Database*/28mysql_query ("SET NAMES ' UTF8 '",$this->link);/** Experience Summary, using the MySQL Settings page encoding, it is best to wait for the link, and then set, meaning is used after the library function*/2930if (!$thisLink) {31Exit ("Connect_database_wrong");32}33if (!mysql_select_db ($this->config->db,$thisLink)) {34Exit ("Select_db_wrong");35}36}3738/**39* command, whether to determine the number of rows, whether an error is automatically enabled rollback, chained commits40*/41Publicfunctionexec$query,$judgeLength =False$rollBack =False$isCommit =False){42$res =mysql_query ($query,$thislink);43If$judgeLength) {/** Whether to determine the number of rows*/44IfMysql_num_rows ($res) <=0){45ReturnNull;46}47}Else{48if (!$res){49If$rollBack) {50$thisRollBack ();51}52Exit$query);/** Throws an error in the SQL statement*/53}54}55 if ( $isCommit 56 return $this ;else{58 return $res; 59}60}61 62/** Start Transaction */63 public Function Swbegin () {mysql_query ("Begin", $ This->link); 65}66 67/** rollback */68 Public Function rollBack () {mysql_query ("RollBack", $this->link); 70}71 72/** Submitted Transaction */73 Public Function commit ($getThis =false) {mysql_query ("commit", $this->link), and if ($getThis) {return $this ;}else{78 return null;79}80}81}82;
5, example
1 <?Php2/**3* Created by Phpstorm.4* User: Lin Kuo-hong5* DATE:2015/10/246* time:11:237*/89Include "sql.php";10$sql =NewSQL ();1112/** Insert a batch of data into the AA table and pay attention to selecting the InnoDB engine when building AA table*/13For$i = 0;$i <10;$i + +){14$temp =$i. " K;15$query = "INSERT into AA (A,W) VALUES ('$i ', '$temp ') ";16$sqlexec$query);17}1819/** The following comments please open your own view effect*/20//$sql->rollback (); /** Rollback, all inserts above will not be executed */21st//$sql->commit (); the insert will not be executed if/** does not commit */ 22 23 /** Select operates without a commit or directly using the result set */24 /*25 $result = $sql->exec ("select * from AA"); 26 while ($row = Mysql_fetch_assoc ($result)) {27 print ($row). " </br> "; 28 }29 */30 31 "
Mysql-innodb-Transaction, PHP version