Home  >  Article  >  Backend Development  >  php事务控制问题

php事务控制问题

WBOY
WBOYOriginal
2016-06-06 20:18:381483browse

php事务控制一定需要这样控制的吗?

if($sql1&& $sql2 && $sql3)
{

<code>$tranDb->commit();</code>

}

这样sql多了,if的条件会很多$sql1&&$sql2,不同的函数之间如何控制事务?

回复内容:

php事务控制一定需要这样控制的吗?

if($sql1&& $sql2 && $sql3)
{

<code>$tranDb->commit();</code>

}

这样sql多了,if的条件会很多$sql1&&$sql2,不同的函数之间如何控制事务?

应该通过捕获异常的方式提交事务或事务回滚。

if (!$sql1){
$db->rollback();
}
if (!$sql2){
$db->rollback();
}
if (!$sql3){
$db->rollback();
}

$db->comit();
就是全部成功后再提交,一有失败就回滚,手机码字,希望能看懂!

楼上的看起来清晰,但冗余也太多了。 题主的写法不错,目前我也是这么写的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn