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.
  • 2. Strict Mode.
  • 3. Managing Errors.
  • 4. Disabling Transactions.
  • 5. Test Mode.
  • 6. Running Transactions Manually.

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:-

Codeigniter Transactions Reference

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:-

Codeigniter Transactions Reference

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:-

Codeigniter Transactions Reference

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:-

Codeigniter Transactions Reference

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();
}
}
}
?>

Advertisements

Add Comment

📖 Read More