Friday, October 21, 2011
Wednesday, October 19, 2011
How MySQL Transaction works ?
A transaction is a sequential group of database manipulation
operations, which is performed as if it were one single work unit. In
other words, a transaction will never be complete unless each individual
operation within the group is successful. If any operation within the
transaction fails, the entire transaction will fail.
Practically you will club many SQL queries into a group and you will execute all of them together as a part of a transaction.
You can not use transactions directly, you can but they would not be
save and guaranteed. If you plan to use transactions in your MySQL
programming then you need to create your tables in a special way. There
are many type of tables which support transactions but most popular one
is InnoDB.
You can execute these SQL commands in PHP by using mysql_query() function.
- Begin transaction by issuing SQL command BEGIN WORK
- Issue one or more SQL commands like SELECT, INSERT, UPDATE or DELETE
- Check if there is no error and everything is according to your requirement.
- If there is any error then issue ROLLBACK command otherwise issue a COMMIT command.
Those who have no idea about the MySQL Transactions can take a look at the below code.
It will help you to get an overall idea about how a transaction works in MySQL.
mysql_query("START TRANSACTION"); $a = mysql_query("INSERT INTO table_name (amount) VALUES('100')"); $b = mysql_query("INSERT INTOtable_name(amount) VALUES('200')"); if ($a and $b) { mysql_query("COMMIT"); } else { mysql_query("ROLLBACK"); }
Subscribe to:
Posts (Atom)
Popular Posts
-
Some times when you connect to the webhost via FileZilla you may encounter this error message. "421 Sorry, cleartext sessions are ...
-
Q: Can I start my own company on oDesk? A: Yes! oDesk allows you to build and manage a team of workers, colleagues and collaborators. Yo...
-
Now i'm telling you on how to add a rich text editor inside a Text/HTML widget in a Wordpress Admin Panel - Widget Area. Here we are usi...
-
Are you a stock trader looking to learn and earn? Look no further than NSE Trading Alerts, the ultimate resource for trading setups and idea...
-
global $COURSE, $USER; $context = get_context_instance(CONTEXT_COURSE,$COURSE->id); if (has_capability('moodle/legacy:student', ...
-
In Joomla most probably after upgradation users will encounter an error, that is they cannot edit articles or sections. You can fix this b...
-
The CSS frameworks here offer designers a structure going into their projects, so they aren’t starting entirely from scratch. Some offer lay...
-
When adding a fancybox popup you may encounter a common issue, which shows the below error message, while clicking the link. "Th...
-
Most of the readers are familiar with the International Space Station or "ISS" as it’s often referred to. But just in case you ...
-
DivX Crawler is the first and most reliable source for Direct Download Movies, Software and Music Videos. Username : divx273 Password : 8...