Saturday, March 17, 2012

How to use Mysql Transactions with PHP

Web applications are more popular today than ever with the increasing number of internet users. Most of the standard alone applications converted as web based applications or at least they try to provide a web interface for users. PHP and Mysql are two leading technologies which allow uses on rapid development of web based systems. "Transaction" is a powerful concept which comes with Mysql 4.0 and above versions. Lets explore that.


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.

  1. Deduct XXX amount form Bobs account.
  2. 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.

  1. Transaction Start
  2. Deduct XXX amount form Bobs account.
  3. Add XXX amount for Alices account.
  4. If both queries run successfully then commit the changes
  5. 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) {
} else {

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.

You have to be aware on these limitations before you use "Transactions" for your application. Have a great time with Mysql "Transactions".

References :

Saturday, March 3, 2012

Select the Best Training Place

"Your work is the presentation of your capabilities". You need to find the correct job on correct time. There are several milestones which are considered as the hardest decisions you have to make ever in your carrier path. Those are
  • Choosing your first industrial training place
  • Choosing your first job
Out of those two, choosing your first job is also highly influenced by your industrial exposure you earned at training place. You have to make the correct decision on that, or else you'll suffer from the bad influences of your decision through the entire life. It's not an easy task to do that correctly. You have to spend days and days to take the right decision. In this post I try to point you some important facts which will help you to make the correct decision on choosing the best fit training place for you.

As an undergraduate student you may not have much experience on how your academics related with the industry. Industrial training is the first time you get a chance to go into the industry and feel the reality of the industry. But at this point if you have chosen the wrong place to taste it, you will definitely get lost. When you get the list of opportunities, please consider following factors before shortlisting them.
  • It should accordance with your future carrier.
  • Companies expectations and your expectations should be inline.
  • Nature of the company.
  • Ask your seniors about the company and the culture.
Even though these ideas seems so simple they are very powerful. Following are the explanations of those facts.
  • It should accordance with your future carrier - You need to have a clear idea on what technologies you are going to use in your future carrier. If you have a dream to be a Java programmer in the future, it will have more weight for your selection algorithm. If you need to be a Java programmer do not select something else, such as Web development or Mobile development. Even though all the technologies share some core concepts with them, each technology is using some unique concepts to decorate it. So by choosing the wrong option you will loose some interesting areas in your future plan. As an example if you choose php development for the training you may not need to define your variables at coding. So after couple of months you may loose the practice of defining variables which you gained by Java. It's a simple example and you can find many more crucial things with them.
  • Companies expectations and your expectations should be inline - You need to know what are the expectations the company has on you. If they hire you only because they have some extra works which need to finish early as well as they don't need to let you learn anything new with those works, it will not be the correct option for you. Some companies hire trainees because it is cost effective for them. They don't let trainees to learn new things. They ask trainees to do the internal bug fixings, QA and documentations which will never expose you the the industrial experience. I have seen some students who didn't even experienced on version control systems during their internship. So don't choose that kind of companies for the training. Always choose companies which have higher expectations on trainees as well as have a good vision to help the trainee to improve himself in technology as well as communication.
  • Nature of the company - If you need to learn everything withing a company, the best fit model is a small scale company. Small scale companies allow you to go through all the stages of a project as well as the marketing and sales strategies of those companies. So if you have an idea of having a start up in the future, you should select an small scale company to extract that knowledge. If you need to learn the big processes used withing large companies, you need to select a large scale company as your training place. But in most of the large companies you will not expose to the real business process they use. If you need to work on an opensource company you need to select such a company who work with opensource projects and products, or else you can choose a company which works with closed source projects and products.
  • Ask your seniors about the company and the culture - There is a set of people who always willing to help you. Those are your seniors. So please contact your seniors who are working on those companies. They will definitely tell you the truth about the company unless they are the owners of that company. It's better if your training place has a peaceful culture which don't have unnecessary restrictions and collisions. If you hate working at night for the whole year, you should definitely avoid such companies. Even though it is must to meet the deadlines we cannot do it for all 365 days. If you don't care about night shifts and your only vision is to get the maximum exposure during your training period you better choose a company which expects your hard working.
In all those points I never mentioned about the salary your get. The salary has the minimum weight when comparing with above factors. Your salary in the training periods say nothing about the quality of the training you get. So don't think about salary. Money is not everything and it's only a one thing.

When you face to your fist job interview, they will definitely ask you about the training place. So there should be something to say about your training experience. I cannot say this is the best fitting model for your internship. You have to match your expectations with those factors.

Think Think and Think. Make your decision which will decorate your bright future.

Friday, March 2, 2012

Enjoy your summer with GSOC

Since most of the students ask from me "How to get in to GSOC", I though to write down some important facts which will help you when applying for a GSOC project.

First of all if you need to get more detailed description on GSOC you can refer this set of slides which were prepared by Kathiravelu Pradeeban.

GSOC is full of competition. People who have more commitment on it successfully finish it. It's not a matter of your knowledge on technologies, capacity on academics or fluency on language. Its all about you commitment to that project. It's not just the money you get or the package you receive, it's about your enthusiasm towards the opensource world. So ready your backpack to join the memorable journey.

Selecting an Organization

When you are in the competition, selecting an organization for the project plays a major role in your success path. Most people lost their enthusiasm on the competing as soon as they realize the difficulty on selecting a proper organization. Don't be silly. There are few simple steps you can follow to get rid of this headache.

  • Find out what are the organizations which were acted as mentoring organizations in the last GSOC competition. Most of the organizations which were at the last competition will be repeatedly get selected to the next upcoming sessions. So rather than searching here and there, you can directly refer the last years GSOC web page for the information. You can find last years mentoring organizations in that list. Now you get an idea about what kind of organizations participate for this competition and what are the relevant technologies they use.
  • Now you can shortlist some of the organizations according to your awareness on those technologies. But it is not necessary to be an expert on those technologies. It's okay to select it even though you know nothing about mentioned technologies, you have few more weeks to get ready with that. In this page you can find how many project slots those organizations got last year. If you select organizations which get more slots, you can be assure that your project idea will not get thrown off at the last moment. Now you have couple of targets on your head.
Selecting a Project

Now you are done with selecting organizations. Next step id to select an project idea.
  • Search for the 2012 GSOC ideas of those organizations. As an example if you select PhpMyadmin as an organization, you can google for "phpmyadmin 2012 gsoc ideas". It will give you set of links which are related to that topic. Among those you can find this wiki page which solves half of your problems.
  • Now you can find what are the latest project ideas they have, who are the assigned mentors for those ideas and what are the technologies they expect from you. If those project ideas match with your expectations you can proceed, or else you can find for other organizations. Assume that you are interested on an idea which was in the Phpmyadmin wiki page.
  • Then you can find whether this is a continuation of a previous GSOC project or a new one using the gsoc project list which I have mentioned above. If it is a continuation your are lucky. You have person to ask. So find who was the last student and drop him mail to get more information. It's okay to drop one or two private mails to him for more information but not too much.
Dropping your first mail
  • Since this a project with a community your visibility to that community is a crucial factor when they select students for their project. So your private mails add less advantage on the competition. So find what is the developer mailing list and the IRC channel of that organization. Then subscribe to them. Now you are ready to put your first mail to the organization which will get a reply with a warm welcome to the community.
  • In the first mail you can introduce your self to the community and mention that your are willing to participate on gsoc with that organization. You need to specifically mention the project idea which is in your focus on that mail and ask for more information.
  • Then you'll get a reply with related information, links to students guides and many more useful things for that project. Now your have to read them and follow them to be prepared for the project. As always you can ask questions on the mailing list for more details.
  • Since all the community people help you voluntarily, you should not bother them asking quick replies for your mails. If they have a time they will definitely reply you. So be patient and polite on the IRC and the mailing list.
Continue with the community
  • You have work with the community and impress them before applying for the project. Most of the communities expect students to fix some bugs which are listed on their bug tracks to make sure that the student is committed to this project.
  • So you can select some easy bugs from the bug list and try to resolve them. You can ask for more help on the mailing list.
  • You can suggest your own ideas to the project and discuss them on the mailing list.
Early bird gets the worm
  • Since this is highly competitive, more and more students will try to get the same project. Most of projects get huge number of request at the last moment. So it is better to active on the project earlier and be on safe side.
  • One other advantage of being active on the community is get rid of unnecessary competition. Most of the students look at an idea from the list and then go and check in the mailing list to make sure someone is not active on that project idea. If they see that there are many number of mail threads related to that project which were sent by you, they will think twice to compete with you. They may think it's better to move on to a another project which will be more easy to drag.
Get ready your proposal

By the time of your proposal submission, you'll have sound knowledge on the project as well as the community. It's better to prepare it couple of days before the deadline and get your proposal reviewed by the community and the mentor. It will enhance the quality of your proposal as well as avoid unnecessary changes during the project time.

Now you are done with the proposal submission and if you correctly follow these steps you'll definitely get selected to the project. After that the project success is all depend on your commitment.

We warmly welcome you to GSOC. Enjoy your summer.