PHP simple transaction test cases
Show engines; + Region + --------- + Region + -------------- + ------ + ------------ + | Engine | Support | Comment | Transactions | XA | Savepoints | + Region + --------- + Region + -------------- + ------ + ------------ + | FEDERATED | NO | Federated MySQL storage engine | NULL | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | MyISAM | YES | MyISAM storage engine | NO | BLACKHOLE | YES |/dev/null storage engine (anything you write to it disappears) | NO | CSV | YES | CSV storage engine | NO | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | ARCHIVE | YES | Archive storage engine | NO | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | + -------------------- + --------- + Upper + ------------ + ------ + ------------ + // schemalcreate table transaction_tbl (name char (10) not null) engine = innodb; */class Transaction {// determines whether the inserted query is successful. private $ error = 0; // connects to the Resource private $ conn_id = null; public function _ construct () {$ this-> connect ();} public function test () {echo "begin test \ n "; // start transaction $ this-> beginTransaction (); // Insert test data // The name size limit is 10 $ str1 = str_repeat ("a", mt_rand (1, 13); $ str2 = str_repeat ("B", mt_rand (1, 13); $ this-> query ("INSERT INTO 'transaction _ TBL' ('name ') VALUES ('$ str1') "); $ this-> query (" insert into 'transaction _ TBL' ('name') VALUES (' $ str2 ')"); // submit the query if ($ this-> error = 0) {$ this-> commit ();} else {$ this-> rollback ();} echo "end test \ n";} private function query ($ SQL) {echo $ SQL. "\ n"; (mysql_query ($ SQL, $ this-> conn_id) = FALSE) & ++ $ this-> error ;} // start the private function beginTransaction () {mysql_query ("BEGIN", $ this-> conn_id);} // roll back the private function rollback () {echo "ROLLBACK \ n"; mysql_query ("ROLLBACK", $ this-> conn_id);} // submit private function commit () {echo "save done \ n "; mysql_query ("COMMIT", $ this-> conn_id);} // connect to the database private function connect () {$ this-> conn_id = @ mysql_connect ("127.0.0.1 ", "root", "") or die ("can \'t connect :". mysql_error (); mysql_select_db ("test", $ this-> conn_id) or die ("can \'t use test :". mysql_error ();} // close the data private function close () {mysql_close ($ this-> conn_id);} public function _ destruct () {$ this-> close () ;}$ trans = new Transaction (); $ trans-> test ();