php事务回滚

php事务回滚

1
2
3
4
5
6
7
8
9
<?  //示例
try {
    //开启事务 start transaction
    
    //提交事务 commit
} catch (\Exception $e) { //错误消息 $e->getMessage()
    //回滚事务 rollback

}

TP5写法

1
2
3
4
5
6
7
8
9
10
11
<?php
try {
    Db ::startTrans();
    //SQL语句
    Db::commit();
    return json_encode($result);
} catch (\Exception $e) {
    // 回滚事务
    Db::rollback();
    return json_encode($result);
}

原生PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
try {
    mysql_query('start transaction');
    //SQL语句
    mysql_query('commit');
    return json_encode($result);
} catch (\Exception $e) {
    // 回滚事务
    $log_ops ->log_insert("验货操作异常 ".var_export($e->getMessage(),true));
    mysql_query('rollback');
    $result["errcode"] = -1;
    $result["errmsg"] = "数据错误";
    return json_encode($result);
}

PHP抛异常

1
throw new Exception(Value must be 1 or below);