Codeigniter Transaction Example

This tutorial will show you how we can use transaction in Codeigniter. We know that transaction is an important part of persistence storage media because we may save, update or delete data from persistence storage area. For example, in database we may need to update multiple tables or we may require to delete from multiple tables, hence we may face some inconsistency during database tables updation or deletion from database tables. During updation few tables may be updated and few tables may not be updated. Similarly, in case of deletion few tables may get deleted and few may not. So data will be in inconsistent state. Therefore, we want either a unit of work will be carried out completely or none. Hence the transaction is required to make it happen.
For information on Codeigniter transaction please go through


Knowledge of PHP & Codeigniter
MySQL 5.x
Apache HTTP server 2.2
Codeigniter 2.1.4

Now look at the steps how transaction works in Codeigniter

Step 1. Create MySQL tables and these tables will be used for updation or deletion of data.

Step 2. Create usermodel.php file for updating to or deleting data from database

That’s all. Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Twitter Facebook  Google Plus Linkedin

9 Replies to “Codeigniter Transaction Example”

  1. Hello Soumitra,
    Thanks for all your examples

    Is there any continuation for the above tutorial..? if yes please help us in CI itself.

  2. Hello,

    What if I need to use the delete function somewhere inside the update function?
    I mean how to use a transaction enabled function inside another transaction enabled function. I tried and it fails.

    1. why do you need to use another transaction enabled function inside other and if you need to use a function from transaction enabled function then why do you need to make the called function transactional?

      1. Hi,
        Thanks for your attention. Let me explain.

        Suppose there is a function that creates new “Task” that belongs to a particular “Project”. We have a form that submits the task details the user who is assigned to it, and stores task details to task_table and some other tables that stores the relations between task and project or the users table that are assigned to this task for example. So I put it in the transaction rollback. This is independent now.

        When I create a new project, it may be needed to store the project id to some third table. Also I need to create some default tasks that will be related to this particular project. Now the project function has multiple transactions. It will rollback the independent task query but the project will not be rolled back and we have created a project without those default tasks.

        I hope you got my point.

        Thanks again,

  3. Sis,

    I’m new in php and ci but I’m still learning from online resources. Can you create a full tutorial in CRUD using transaction. Thanks for helping 🙂

    1. You need transaction on write, update and delete but not in read. So in this example you will find transactions on update and delete, similarly you can apply on write. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *