mysqli 更新&插入操作及事务

$mysqli = new mysqli('localhost', 'root', '1234', 'shop');
$query1 = " update sw_goods set goods_name='坚果手机plus' where goods_id=118 ";
$query2 = "insert into sw_goods (goods_name,goods_price,xx)values('锤子手机',1888)";

$mysqli->autocommit(false); //关闭自动提交功能,等commit()时才提交

$result1 = $mysqli->query($query1);
$rows1 = $mysqli->affected_rows;

$result2 = $mysqli->query($query2);
$rows2 = $mysqli->affected_rows;

if ($result1 && $rows1 > 0 && $result2 && $rows2 > 0) {
    $mysqli->commit(); //提交
    echo 'ok';
} else {
    $mysqli->rollback(); //回滚
    echo 'failed';
}

$mysqli->autocommit(TRUE);
$mysqli->close();

 

posted @ 2018-04-19 16:44  jiafeng  阅读(278)  评论(0编辑  收藏  举报