Home > Codeigniter > Use PEAR MDB2 In Codeigniter

Use PEAR MDB2 In Codeigniter

I use codeigniter frequently for my project, but what i always need to do is use Pear MDB2 insist of codeigniter ADR, so below is some simple step by which you can use PEAR MDB2 in your codeigniter framework.

Step 1 : Install Pear and add pear folder in your php.ini files include option.

Step 2 : if you already added the database in your autoload.php file for autoloading of your database so pls remove it.

Step 3 : Create one directory named ‘pear’ in application folder.

Step 4 : Unzip the attached file in the same folder i.e :


what is in the attachment?? nothing but just a MDB2 files with all supported database driver, provided by pear.php.net with latest version. So that’s you wont need to copy each driver separately in that folder.
Now your file structure like

application/pear/MDB2/MDB2.php //(mdb2 class file)
application/pear/MDB2/MDB2/ //(all database driver in it)

Step 5 : Create file named ‘Mdb2loader.php’ in application/libraries folder.
Add below code in the same file

if (!defined('BASEPATH')) exit('No direct script access allowed');
class Mdb2loader{
function connectdb(){
require_once "".APPPATH."/pear/MDB2/MDB2.php";
$dsn = 'mysql://username:password@localhost/database_name';
$mdb2 =& MDB2::factory($dsn);
if (PEAR::isError($mdb2)) {
return $mdb2;

HERE adjust $dsn value as per your setting

Setp 6 : Create new controller name Welcome
Add below codes in that file

class Welcome extends Controller {
private $db2;
function __construct()
return $this->db2 = $this->mdb2loader->connectdb();

function index(){
$sql = $this->db2->query(‘SELECT * FROM db_table’);
if (PEAR::isError($sql)) {

echo “<table>”;
while (($row = $sql->fetchRow())) {
echo “<tr>”;
echo “<td>”.$row[‘0’].”</td>”;
echo “<td>”.$row[‘1’].”</td>”;
echo “<td>”.$row[‘2’].”</td>”;
echo “</tr>”;
echo “</table>”;

Adjust the database table name in above query.

Done :: now check the working status of your controller

Download : Pear MDB2 Package

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: