How to connect to SQL Server database in CodeIgniter?

How to connect to SQL Server database in CodeIgniter?

I am currently running an application in CodeIgniter and I would like to use SQL Server.

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '#.#.#.27';
$db['default']['username'] = '@@@@@@';
$db['default']['password'] = '@@@@@@@@@';
$db['default']['database'] = '$$$$$$$$$$$$$';
$db['default']['dbdriver'] = 'mssql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

      

After auto-loading the database, only a blank white page with no errors is displayed. Could you please tell me what other changes I need to make to work with the SQL Server database?

#autoload.php#
$autoload['libraries'] = array('database');

      

+3


source to share


7 replies


use use $ db ['default'] ['dbdriver'] = 'sqlsrv';
and install Microsoft Drivers for PHP for SQL Server on windows



CodeIgniter MSSQL Connection

+3


source


I couldn't get it to work with a supported driver mssql

, so I usedsqlsrv

I usually connect to ip, port like hostname

, so I changed that.

pconnect

was also giving me problems. I installed it in FALSE

and started working.



Here is the configuration I got:

$db['default']['hostname'] = '127.0.0.1,1433';
$db['default']['username'] = 'username1';
$db['default']['password'] = 'secretpassword';
$db['default']['database'] = 'databasename';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

      

+3


source


How about this? I've seen it work on some servers. Is it a Windows server?

$db['default']['dbdriver'] = "odbc";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE; 

      

following reading: http://codefury.net/2011/01/codeigniter-php-iis-mysql-mssql/

hope this helps

0


source


The configuration code is fine. Further reading here

IF you think the errors are not showing, then go to index.php

and the following snippet will appear at the top to show the errors.

error_reporting(E_ALL);

      

Other, then check if the MSSQL service is running and available. Can be set up simple .php

and try connecting with regular codes .

File without CI Something like

<?php
$server = 'YOURPC\SQLEXPRESS';
$link = mssql_connect($server, 'user', 'pass');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
?>

      

0


source


this is the same process as mysql. use the same configuration as below:

$ active_group = 'default'; $ active_record = TRUE;

$ db ['default'] ['hostname'] = 'xxx.xx.xx.xx \ SQLEXPRESS';

$ db ['default'] ['username'] = 'sa';

$ db ['default'] ['password'] =

'XXXXXXX'; $ db ['default'] ['database'] = 'xxxxx';

$ db ['default'] ['dbdriver'] = 'mssql';

$ db ['default'] ['dbprefix'] = '';

$ db ['default'] ['pconnect'] = TRUE;

$ db ['default'] ['db_debug'] = TRUE;

$ db ['default'] ['cache_on'] = FALSE;

$ db ['default'] ['cachedir'] = '';

$ db ['default'] ['char_set'] = 'utf8';

$ db ['default'] ['dbcollat'] => 'utf8_general_ci';

$ db ['default'] ['swap_pre'] = '';

$ db ['default'] ['autoinit'] = TRUE;

$ db ['default'] ['stricton'] = FALSE;

In your model use the same as

selectagent function ($ table, $ agent) {

$ field = $ this-> db-> get_where ($ table, array ('var1' => $ Agent, 'day (date)' => date ('d')));

  $fields=$field->result();

  return $fields;
}

      

0


source


I just solved this problem. And I was connecting to MSSQL hosted with microsoft Azure

The steps I followed after doing some research on the internet are as follows:

database.cfg:

$db['default']['hostname'] = 'XXXXXXX.database.windows.net';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'databasename';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

      

Basically dbdriver, pcconnect and hostname you have to set up correctly. Rest is common. Get hostname from your azure database data.

And I also modified a couple of system files since I heard about DB driver issues.

System / databases / drivers / sqlsrv / sqlsrv_driver.php

function db_pconnect()
    {
        //$this->db_connect(TRUE);
        return $this->db_connect(TRUE);
    }

      

and

function affected_rows()
{
    //return @sqlrv_rows_affected($this->conn_id);
    return @sqlsrv_num_rows($this->result_id);
}

      

I was able to connect to the database and create a database application.

Hope this helps someone needing it :)

0


source


Follow these simple steps:

  • Install an instance with "mixed mode" -enabled. If you are unsure of watching this - the video starts with the correct sequence. Maybe it's a good idea to watch it in full.

  • change db-config to '... ci / application / config / database.php'

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn' => '',
        'hostname' => 'localhost',
        'username' => 'sa', // <- use 'sa'
        'password' => 'THE_PASSWORD_YOU_SET_ON_INSTALL', // "mixed-mode"
        'database' => 'ci',
        'dbdriver' => 'sqlsrv',
    //  'dbdriver' => 'mysqli', // <- my old "non-server" config (approx. 70% slower)
        '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
    );
    
          

enter image description here

0


source







All Articles