Connect to MS SQL on (Linux)Centos with Codeigniter Connect to MS SQL on (Linux)Centos with Codeigniter codeigniter codeigniter

Connect to MS SQL on (Linux)Centos with Codeigniter


You need to install php-mssql extension on your linux machine.

Step 1: Install needed respositories

Remi Dependency on CentOS 6 and Red Hat (RHEL) 6

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpmrpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Remi Dependency on CentOS 5 and Red Hat (RHEL) 5

rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpmrpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

Step 2: Install the extension

yum --enablerepo=remi php-mssql

Step 3: Enable extension

Edit your php.ini and load the php_mssql library and restart your Apache web server.


Codeigniter_project\application\config\database.php

 $active_group = 'default';    $query_builder = TRUE;$db['default'] = array(    'dsn'   => '',    'hostname' => 'localhost',    'username' => 'admin',    'password' => 'Admin@321',    'database' => 'xxxx',    'dbdriver' => 'mysqli',    //'dbdriver' => 'mysql',    'dbprefix' => '',    'pconnect' => FALSE,    'db_debug' => (ENVIRONMENT !== 'production'),    'cache_on' => FALSE,    'cachedir' => '',    'char_set' => 'utf8',    'dbcollat' => 'utf8_general_ci',    'swap_pre' => '',    'encrypt' => FALSE,    'compress' => FALSE,    'stricton' => FALSE,    'failover' => array(),    'save_queries' => TRUE);$db['posdb']['hostname'] = 'xxx.xxx.xxx.xxx:1433';//$db['posdb']['port'] = 1433; // or the port you configured on step 6$db['posdb']['username'] = 'sa';$db['posdb']['password'] = 'myself@123';$db['posdb']['database'] = '

Blockquote

new';

$db['posdb']['dbdriver'] = 'mssql';$db['posdb']['dbprefix'] = '';$db['posdb']['pconnect'] = TRUE;$db['posdb']['db_debug'] = FALSE;$db['posdb']['cache_on'] = FALSE;$db['posdb']['cachedir'] = '';$db['posdb']['char_set'] = 'utf8';$db['posdb']['dbcollat'] = 'utf8_general_ci';$db['posdb']['swap_pre'] = '';$db['posdb']['autoinit'] = TRUE;$db['posdb']['stricton'] = FALSE;

Consider posdb be the second database which is going to connect with remote mssql at 2012

To get this in Controller is

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class Contrller_name extends CI_Controller{         public function __construct()    {      parent::__construct();           $this->load->model('my_model');      $this->load->library('session');        }    public function connet(){               $connnet =  $this->my_model->connet();        echo "<pre>";        print_r($connnet);        //var_dump($connnet);    }}   ?>

Now at model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class My_model extends CI_Model{     function __construct()     {                   parent::__construct();          $this->load->helper('date');          $this->pos_db= $this->load->database('posdb', TRUE);  // loading secondary remote database      }     function connet(){                $sql ="SELECT TOP 10 * FROM dbo.Party";    $query = $this->pos_db->query($sql);    $result = $query->result_array();    return $result;     }}?>

This is worked for me most of times


It is currently working for my by installing mssql at the Linux machine. Codeigniter has the following configuration:

$db['sql']['dbdriver'] = 'mssql';$db['sql']['dbprefix'] = '';$db['sql']['pconnect'] = TRUE;$db['sql']['db_debug'] = FALSE;$db['sql']['cache_on'] = FALSE;$db['sql']['cachedir'] = '';$db['sql']['char_set'] = 'utf8';$db['sql']['dbcollat'] = 'utf8_general_ci';$db['sql']['swap_pre'] = '';$db['sql']['autoinit'] = TRUE;$db['sql']['stricton'] = FALSE;

PHP configuration:enter image description here

FreetDs config on Linux server

It is important to configure the connection on the Linux machine in the FreetDS config. After that you will see in the phpinfo that there is an Active connection an Codeigniter can connect with the SQL database.

enter image description here