如何避免在php中执行一半代码


How to avoid half code executing in php

我有一个php脚本,其中包含几个mysql查询。一旦我运行这个脚本,它就会执行前两个查询,并且会出现服务器错误。(任何地方都会发生)。不执行其他查询。这个案子成了一个大问题。因为有些表已更新,而另一些表未更新。我想避免这个问题。他们有解决方案吗。。?

您可以在代码中使用这些查询

START TRANSACTION or BEGIN start a new transaction.
COMMIT commits the current transaction, making its changes permanent.
ROLLBACK rolls back the current transaction, canceling its changes.
SET autocommit disables or enables the default autocommit mode for the current session.

有关的更多信息http://dev.mysql.com/doc/refman/5.7/en/commit.html

我的建议是使用第一个查询作为START Transaction来启动代码,然后执行查询,最后使用COMMIT

阅读MySQLi事务,提交和回滚

http://php.net/manual/en/mysqli.begin-transaction.php

如果您开始交易,这将解决您的问题。然后,您可以执行所需的所有查询,然后提交。如果提交从未被触发,那么将自动回滚。