实物回滚
Posted Zero_oreZ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实物回滚相关的知识,希望对你有一定的参考价值。
$m
=D(
‘YourModel‘
);
//或者是M();
$m2
=D(
‘YouModel2‘
);
$m
->startTrans();
//在第一个模型里启用就可以了,或者第二个也行
$result
=
$m
->where(
‘删除条件‘
)->
delete
();
$result2
=m2->where(
‘删除条件‘
)->
delete
();
if
(
$result
&&
$result2
){
$m
->commit();
//成功则提交
}
else
{
$m
->rollback();
//不成功,则回滚
}
$servername
=
"localhost"
;
$username
=
"root"
;
$password
=
"admin"
;
$dbname
=
"test"
;
try
{
$conn
->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//开始事务
$conn
->beginTransaction();
$conn
->
exec
(
"INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES(‘YE‘,‘XIAMING‘,‘[email protected]‘)"
);
$conn
->
exec
(
"INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES(‘YE‘,‘CONG‘,‘[email protected]‘)"
);
$conn
->
exec
(
"INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES(‘FANG‘,‘MENG‘,‘[email protected]‘)"
);
//提交事务
$conn
->commit();
echo
"New records created successfully!"
;
}
catch
(PDOException
$e
){
//回滚事务
$conn
->rollBack();
echo
$sql
.
"<br>"
.
$e
->getMessage();
}
$conn
=NULL;
以上是关于实物回滚的主要内容,如果未能解决你的问题,请参考以下文章