Web based applications have different types of requirements and some of them prefer more consistency on its data than others. As an example Banking software expect its data to be more secure and consistency. Assume a situation where one person (Bob) is transferring some amount of money (XXX) to his friends bank account (Alice) directly form account to account. The queries for the banking software database on this transaction will be as follows.
- Deduct XXX amount form Bobs account.
- Add XXX amount for Alices account.
If both queries executed successfully, it will be great. But assume a situation where the first query executed successfully and then the database connection get lost. At that situation the second query will not run on top of that database, so nothing will add to the Alices account. The deducted amount form the Bobs account will also not get recovered. These will make a disaster for Bob, Alice and for the Bank.
"Transactions" can solve this problem with a great solution which will ensure the consistency of the database states. Simply with Transactions the database can be role back to the initial state where it was before the deduction query executed on the database. Its like this.
- Transaction Start
- Deduct XXX amount form Bobs account.
- Add XXX amount for Alices account.
- If both queries run successfully then commit the changes
- Otherwise role back to the initial state.
For these operations Mysql provide several commands with it and lets see how we can implement that with PHP.
For support Mysql transactions PHP 5.0 and above provides set of methods which are very similar to the normal mysql functions but with an additional "i" letter with them. The most important functions out of them are as follows.
- mysqli_connect() - connect with mysql
- mysqli_select_db() - select a database
- mysqli_autocommit() - enable and disable the auto commit option
- mysqli_query() - run the mysql query
- mysqli_rollback() - role back the database to the initial status
- mysqli_commit() - commit all the changes to the database
- mysqli_close() - close the mysql connection
Lets look at how you implement a transaction as a solution for the above problem.
Since transaction only supports from InnoDB storage engine you have to make sure all the database table are with InnoDB engine in your database.
$host = 'localhost';
$user = 'username';
$password = 'password';
$db = 'transaction';
$con = mysqli_connect($host, $user, $password);
mysqli_select_db($con, $db);
//Cancel auto commit option in the database
mysqli_autocommit($con, FALSE);
//You can write your own query here
$query1 = "query for deduct XXX amount form Bobs account";
$results[] = mysqli_query($con, $query1);
//You can write your own query here
$query2 = "query for add XXX amount for Alices account";
$results[] = mysqli_query($con, $query2);
$sucess = true;
foreach( $results as $result) {
if(!$result) {
$sucess = false;
}
}
if(!$sucess) {
mysqli_rollback($con);
} else {
mysqli_commit($con);
}
mysqli_close($con);
In above code the changes will done temporary because we have disabled the auto commit option in the database. After the two queries executed it will check for the results of those queries. If some thing went wrong with the database connection in the middle of the operations the changes will not be permanently applied to the database. And at the end it checks for the success of all queries and if all went fine it will commit the changes to the database.
With this code you will be able to make a consistent database operation for the money transfer action in your database. So your done with set of great consistent database operations on your database with PHP and Mysql.
Still Mysql allows only few operations such as update, insert and delete to be role back using its "Transactions". Following operations cannot be rolled back using Mysql.
- CREATE DATABASE
- ALTER DATABASE
- DROP DATABASE
- CREATE TABLE
- ALTER TABLE
- DROP TABLE
- RENAME TABLE
- TRUNCATE TABLE
- CREATE INDEX
- DROP INDEX
- CREATE EVENT
- DROP EVENT
- CREATE FUNCTION
- DROP FUNCTION
- CREATE PROCEDURE
- DROP PROCEDURE
You have to be aware on these limitations before you use "Transactions" for your application. Have a great time with Mysql "Transactions".
References :


You might also want to investigate PDO-based Transaction handling, as many are using that interface today. It being a database-independent interface, it throws exceptions if the underlying driver does not support transactions.
ReplyDeleteThanks Dan. I'll go through that. I'm not experienced it yet.
DeleteNice post. Parents are parents, not Gods. Of course they cannot have all the answers. The way you explain things to your kids should do all the work…it all comes under parenthood and your people skills.
ReplyDeletePHP Traning
Thanks sumit. :)
DeleteYeah, you are right there are so many databases available that we can use. MySql is one of them. it is defined in backend databases and mainly use with PHP.
ReplyDeleteHTML Training in Jaipur
Very nice post Mr.Thilanka Kaushalya,
ReplyDeleteWith Transactions these two guys(Bob and his friend) wont become enemies
Really your content is so informative. So please share some more content ..
ReplyDeletePHP course in Delhi
Detection of water leaks in Madinah*Cleaning in Riyadh*Transfer to Riyadh is cheap*Home Services in Riyadh*Insulation Foam in Riyadh*Detection of water leaks in Dammam*Fighting insects in Riyadh*Insulation of roofs in Riyadh*Detection of water leaks in Riyadh*Sewage cleaning in Riyadh
ReplyDeleteThanks to this information wonderful
ReplyDeleteشركة مكافحة حشرات بخميس مشيط
شركة مكافحة حشرات ببريدة
شركة مكافحة حشرات بالقصيم
شركة مكافحة حشرات بأبها
شركة مكافحة حشرات بجازان
شركة مكافحة حشرات بنجران
شركة اللمسة الأخيرة تقدم لك الحل الأمثل فلا حشرات بعد اليوم ولن تعود مرة أخرى. فنحن نستخدم أفضل المبيدات العالمية الفعالة صديقة البيئة التي لا تترك رائحة ولا سيوثر على صحة الأنسان ويقوم باستخدامها عمال مدربون يقومون برش المبيدات بشكل علمي مما يضمن لك الراحة التامة نرجو التواصل على هذا الرقم 0580002467
ReplyDeleteشركة رش مبيدات بأبها
شركة مكافحة حشرات بأبها
شركة مكافحة النمل الابيض بأبها
شركة رش مبيدات بخميس مشيط
شركة مكافحة حشرات بخميس مشيط
شركة مكافحة النمل الابيض بخميس مشيط
شركة رش مبيدات بالقصيم
شركة مكافحة حشرات بالقصيم
شركة مكافحة حشرات بجازان
شركة رش مبيدات بجازان
شركة اللمسة الأخيرة تقدم لك الحل الأمثل فلا حشرات بعد اليوم ولن تعود مرة أخرى. فنحن نستخدم أفضل المبيدات العالمية الفعالة صديقة البيئة التي لا تترك رائحة ولا سيوثر على صحة الأنسان ويقوم باستخدامها عمال مدربون يقومون برش المبيدات بشكل علمي مما يضمن لك الراحة التامة نرجو التواصل على هذا الرقم 0580002467
ReplyDeleteشركة رش مبيدات بأبها
شركة مكافحة حشرات بأبها
شركة مكافحة النمل الابيض بأبها
شركة رش مبيدات بخميس مشيط
شركة مكافحة حشرات بخميس مشيط
شركة مكافحة النمل الابيض بخميس مشيط
شركة رش مبيدات بالقصيم
شركة مكافحة حشرات بالقصيم
شركة مكافحة حشرات بجازان
شركة رش مبيدات بجازان
Hire ruby developers trusted by the world's leading brands.
ReplyDeleteهل تبحث عن شركة متخصصة فى خدمات التنظيف بالطائف بافضل المعدات والسوائل وثقة تمة فى العمل ودقة فى النتائج كل هذه المميزت توفرها شركة الخليج الشركة الافضل والامثل فى الخدمات المنزلية بالطائف وبما اننا الشركة الافضل والامثل بدون منافس سوف نسعى لتوفر افضل الخدمات باقل تكلفة وبقدر كبير من الاهتمام والدقة عزيزى اينما كنت فى اى منطقة ا وحى تابع لمدينة الطائف اتصل بنا وسوف نصلك فى الحال شركة الخليج للخدمات المنزلية شركة تنظيف منازل بالطائف
ReplyDeleteشركة تنظيف فلل بالطائف
شركة تنظيف خزانات بالطائف
شركة تسليك مجارى بالطائف
شركة رش مبيدات بالطائف
شركة مكافحة نمل ابيض بالطائف
شركة مكافحة حشرات بالطائف
شركة عزل اسطح بالطائف
شركة عزل خزانات بالطائف