How to rollback the last executed mysql query.Suppose there are 2-3 queries depend upon submit button and first query run successfully and second failed then how to rollback first query..
-
You can use mysql transactions. http://stackoverflow.com/questions/2708237/php-mysql-transactions-examples – Vikas Umrao Feb 28 '14 at 05:09
3 Answers
Use mysql_rollback
or mysqli_rollback
depending on whether you are using mysql
or mysqli
extension
This link should give you a clearer idea

- 2,294
- 2
- 20
- 28
You need to use transactions.
Sequence will be :
1) Begin the transaction 2) Issue one or more SQL commands 3) See if there is any error or not 4) If there is error then issue ROLLBACK if not COMMIT
Note : By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent. To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement which will make autocommit disabled until you end the transaction with COMMIT or ROLLBACK.

- 258
- 2
- 6
Use transactions:
START TRANSACTION;
SELECT * FROM tbl_foo;
SELECT * FROM llsldllfdlfdld; -- typo, fails!
ROLLBACK; -- reverses first transaction
If everything works, instead of ROLLBACK
, send COMMIT;
to make the changes final.

- 44,308
- 16
- 74
- 100