How do I connect to a SQL Server database in CodeIgniter?

前端 未结 7 1759
轻奢々
轻奢々 2020-12-30 17:17

How do I connect to a SQL Server database in CodeIgniter?

I\'m currently starting an application in CodeIgniter and I would like to use SQL Server.

$         


        
相关标签:
7条回答
  • 2020-12-30 17:50

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

    Steps I followed after several research work on 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;
    

    Mainly dbdriver,pcconnect and the hostname you should configure properly. Rest are common. Get hostname from your azure database details.

    And I also modified couple of system files as I heard there were issues with DB driver.

    system/database/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 database and create database application.

    Hope it will help someone in need :)

    0 讨论(0)
  • 2020-12-30 17:56

    Your Configuration code is fine. Further reading here

    IF you believe the errors are not showing up, then go to the index.php and on the top place the following snippet to show the errors.

    error_reporting(E_ALL);
    

    Other, then this check if the MSSQL service is running and is accessible. May be create a simple .php and try a connection with plain codes.

    A non CI file Something like,

    <?php
    $server = 'YOURPC\SQLEXPRESS';
    $link = mssql_connect($server, 'user', 'pass');
    
    if (!$link) {
        die('Something went wrong while connecting to MSSQL');
    }
    ?>
    
    0 讨论(0)
  • 2020-12-30 18:02

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

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

    further reading

    hope this helps

    0 讨论(0)
  • 2020-12-30 18:04

    I couldn't get it to work with the supported driver of mssql so I used sqlsrv

    I normally connect with ip,port as the hostname, so I changed that.

    pconnect was also giving me issues. I set it to FALSE and it started working.

    Here is the configuration I got to work:

    $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;
    
    0 讨论(0)
  • 2020-12-30 18:05

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

    CodeIgniter MSSQL connection

    0 讨论(0)
  • 2020-12-30 18:11

    it is the same process as mysql. use the same configuration given 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 same as

    function selectagent($table,$agent) {

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

      $fields=$field->result();
    
      return $fields;
    }
    
    0 讨论(0)
提交回复
热议问题