Tutorialsplane

Codeigniter Transactions Reference


Codeigniter Transactions Reference – This database reference provides many functions that allows you to use transactions with databases that support transaction-safe table type. In MySQL, you will need to be running InnoDB or BDB table type rather than the more common MY|SAM. Most other database platform support transactions natively. Here in this tutorial, we are going to explain how to use transactions reference.


Codeigniter Transactions Reference | Example.

Let us understand how transactions reference works in codeigniter with examples.

Functions:-

There are following functions available in transactions reference. Now we will explain one by one.

1. Running Transactions.

Here is simple demo of running transactions.

Example:-

Syntax of running transactions.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class transaction_controller extends CI_Controller 
{
public function runningtransaction()
{
$this-?>db->trans_start();
$this->db->query('select * from blog');
if($this->db->query('select * from blog'))
{
	echo "success!";
}
$this->db->trans_complete();
}
}
?>

Output will be like this:-

2. Strict Mode.

Here is simple demo of strict mode.

Example:-

Syntax of strict mode.

$this->db->trans_strict(FALSE);

3. Managing Errors.

Here is simple demo of managing errors.

Example:-

Syntax of managing errors.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class transaction_controller extends CI_Controller 
{
public function errorHandling()
{
$this-?>db->trans_start();
$this->db->query('select * from blog');
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
echo "error";
}
else	
{
echo "Successfully run !";
}			
}
}
?>

Output will be like this:-

4. Disabling Transactions.

Here is simple demo of disabling transactions.

Example:-

Syntax of disabling transactions.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class transaction_controller extends CI_Controller 
{
public function disabletransaction()
{
$this-?>db->trans_off();
$this->db->trans_start();
$this->db->query('select * from blog');
$this->db->trans_complete();
echo $this->db->last_query();
}
}
?>

Output will be like this:-

5. Test Mode.

Here is simple demo of test mode.

Example:-

Syntax of test mode.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class transaction_controller extends CI_Controller 
{
public function TestMode()
{
$this-?>db->trans_start(TRUE); 
$this->db->query("UPDATE blog SET name='Doe' WHERE id=2");
$this->db->trans_complete();
echo $this->db->last_query();
}
}
?>

Output will be like this:-

6. Running Transactions Manually.

Here is simple demo of running transactions manually.

Example:-

Syntax of running transactions manually.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class transaction_controller extends CI_Controller 
{
public function Manually()
{
$this-?>db->trans_begin();
$this->db->query('select * from blog');
$this->db->query("UPDATE blog SET name='Doe' WHERE id=2");
if ($this->db->trans_status() === FALSE)
{
echo $this->db->trans_rollback();
}
else
{
echo $this->db->trans_commit();
}
}
}
?>

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference