<?php //要么全部执行,要么全部不执行; // put your code here $conn = mysql_connect('localhost','root','') or die ("数据连接错误!!!"); mysql_select_db('test',$conn); mysql_query("set names 'GBK'"); //使用GBK中文编码; //开始一个事务 mysql_query("BEGIN"); //或者mysql_query("START TRANSACTION"); $sql = array(); $sql[] = "INSERT INTO `jos_innotest` (`id`, `username`, `sex`) VALUES (NULL, 'user1', '10');"; $sql[] = "INSERT INTO `jos_innotest` (`id`, `username`, `sex`) VALUES (NULL, 'user2', '20');";//这条我故意写错 $sql[] = "INSERT INTO `jos_innotest` (`id`, `username`, `sex`) VALUES (NULL, 'user3', '30');"; $sql[] = "INSERT INTO `jos_innotest` (`id`, `username`, `sex`) VALUES (NULL, 'user4', '40');"; $r = 1; foreach($sql as $key => $row) { $res = mysql_query($row); if(!$res) {$r = 0;} } if(!$r) {mysql_query("ROLLBACK");}else {mysql_query("COMMIT");} mysql_query("END"); ?>