0

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..

user3359065
  • 77
  • 1
  • 3
  • 12

3 Answers3

1

Use mysql_rollback or mysqli_rollback depending on whether you are using mysql or mysqli extension

This link should give you a clearer idea

http://www.w3schools.com/php/func_mysqli_rollback.asp

anurupr
  • 2,294
  • 2
  • 20
  • 28
1

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.

Refer this

acj89
  • 258
  • 2
  • 6
0

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100