表结构
CREATE TABLE `temp` (
`temp_id` int(11) NOT NULL AUTO_INCREMENT,
`group` char(30) NOT NULL COMMENT '分组',
`controller` char(30) NOT NULL COMMENT '控制',
`action` char(30) NOT NULL COMMENT '操作',
PRIMARY KEY (`temp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php
//在命令行中:默认创建的表类型为MyISAM表类型,是不支持事务的
//在命令行中建表时添加 type=InnoDB ,默认自动提交事务autocommit,不能回滚
//创建连接对象
$mysqlConn = new mysqli("localhost", "root", "123456", "test");
if($error=$mysqli->connect_error){
die("连接数据库失败:" . $error);
}
//设置传输字符编码
$mysqlConn->set_charset("uf8");
//1.关闭自动提交 set autocommit=0;
//2.开启事务 start transaction (mysqli中不需要开启,自动开启)
$mysqlConn->autocommit(false);
//执行sql语句
$sqlStr = "INSERT INTO `temp` (`group`, `controller`, `action`) VALUES('a', 'b', 'c');";
$mysqlConn->query($sqlStr);
//查看数据
$sqlStr = "SELECT * FROM `temp` WHERE `group` = 'a' ";
$selectResult = $mysqlConn->query($sqlStr);
if ($selectResult) {
while ($row = $selectResult->fetch_array(MYSQLI_ASSOC)) {
echo $row['temp_id'] . PHP_EOL;
}
}
//回滚
$mysqlConn->rollback();
//提交
//$mysqlConn->commit();
浙公网安备 33010602011771号