How to connect MSSQL DATABASE in PHP (Using with codeigniter) - IT VIET

Technology Hardware & Solution Provider

Wednesday, June 13, 2018

How to connect MSSQL DATABASE in PHP (Using with codeigniter)

To connect MYSQL or MYSQLI in php is rather common than connetion SQL SERVER.  In my posted, i will using codeigniter for a example. For SQL SERVER in codeigniter, you need need to download some Drivers so that we can use SQL SERVER with other PHP .

The download version must be compatible to your PHP version example
Supported Operating System

  1. Version 4.0 supports PHP 7.0+ on Windows and Linux
  2. Version 3.2 supports PHP 5.6, 5.5, and 5.4 on Windows
  3. Version 3.1 supports PHP 5.5 and 5.4 on Windows
  4. Version 3.0 supports PHP 5.4 on Windows

link :

Step 1. 

Download the Drivers from the below link
Extracted sqlsrv drivers and copy to folder ext in your php version


Add these two line in your php.ini file

RESTART Apache (Wamp)
The Above step will install the required drivers for the php to support SQL SERVER . Only the connection with php application is left. For that we need to change the configuration. In Codeigniter you need to edit the file config/database.php file with the correct credentials and fields like database, port username ,password...

$db['default']['hostname'] = 'IP Sql server';
$db['default']['username'] = 'sa';
$db['default']['password'] = 'mssql';
$db['default']['database'] = 'YourDB';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['options'] = array(PDO::ATTR_TIMEOUT => 5);
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

After you changes in database.php, run you PHP apllication in the browser.


View on Youtube

No comments:

Post a Comment