<?php
//在命令行中:默认创建的表类型为MyISAM表类型,是不支持事务的
//在命令行中建表时添加 type=InnoDB ,默认自动提交事务autocommit,不能回滚
//创建连接对象
$mysqlConn = new mysqli("localhost", "root", "root", "test");
if($error=$mysqli->connect_error){
die("连接数据库失败:" . $error);
}
//设置传输字符编码
$mysqlConn->set_charset("uf8");
//1.关闭自动提交 set autocommit=0;
//2.开启事务 start transaction (mysqli中不需要开启,自动开启)
$mysqlConn->autocommit(false);
$error = 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();
标签:autocommit,--,selectResult,事务处理,error,mysqli,mysqlConn,sqlStr From: https://www.cnblogs.com/WebLinuxStudy/p/17371705.html