tags:

views:

99

answers:

4

What is the use of mysql_rollback() function in PHP?

How is it used? please explain me with some example,

PS: Please Do not give me link to the php.net or, mysql site, I dont need AUTHOR language to understand it, I need the Developer way to understand... I hope u understand...

Thanks in advance.

UPDATE

if i have Updated something in a table can I Reset the previous value using this function?

+1  A: 

This issues a ROLLBACK statement, which rolls back the current transaction.

Marcelo Cantos
+1  A: 

mysql_rollback() rolls back the current transaction. I you are unfamiliar with transactions, I would suggest some Wikipedia reading. It is an extremely useful concept.

Anders Abel
+4  A: 

Transactions can be used in MySQL if you want to execute multiple statements at the same time. You send BEGIN to start a transaction, then send mysql_query-s for the statements you want to execute. These statements are executed if you now send COMMIT or cancelled without any effect if you send ROLLBACK. So, to your additional question: ROLLBACK aborts the queries you have sent before and reverts to the old value in the database. However, once you have committed, you cannot rollback anymore since the transaction is then complete.

Etan
But if I do not Use any transaction concept and simpy update a row, then I need the old value, can I get it by using this function?
OM The Eternity
No. This only works if you started a transaction.
pritaeas
+1  A: 

A transaction is a way to ensure either ALL statements succeed, or none do.

For example, to transfer money from Bob's bank account to Alice, one might do this:

UPDATE accounts SET amount = amount - 100 WHERE name = 'Bob'; UPDATE accounts SET amount = amount + 100 WHERE name = 'Alice';

But imagine if something goes wrong (server crashes because power goes out) after the first statement. The bank now has deducted 100 dollar from Bob, but Alice got nothing!

To avoid that, we use a transaction. As Etan explained: you first BEGIN, then you execute all statements. If all went well, you then COMMIT. Only then the modifications are saved.

If something goes wrong (eg there is no account for Alice) you can ROLLBACK, or the server will ROLLBACK if something goes seriously wrong (ie power goes down).

To answer your UPDATE If you have Updated something in a table you can reset the previous value using this function only if you started with BEGIN and did not COMMIT yet, yes.

Konerak
But if I do not Use any transaction concept and simply update a row, then I need the old value, can I get it by using this function?
OM The Eternity
"To answer your UPDATE If you have Upda...." in reply of this part:I have already executed the query update, i.e. simple Update query, Update has been done, NOW can i use this function mysqk_rollback() in PHP?
OM The Eternity
No, sorry. If you update a row, the old value is lost. Only if you started with BEGIN or AUTOCOMMIT was 0, you can ROLLBACK.
Konerak
Isnt there any way I get that old value back in by described situation?
OM The Eternity
Do you have a backup (a mysqldump? A backup of your server with the database files?)? Are queries logged (mysql log or does your application log?)? If not, probably not...
Konerak
Thanks Konerak For the Support
OM The Eternity