简体   繁体   English

将MSSQL和MySQL与CodeIgniter结合使用

[英]Use MSSQL and MySQL with CodeIgniter

I am trying to connect my application to two databases. 我正在尝试将我的应用程序连接到两个数据库。 One is MySQL and one is MSSQL. 一种是MySQL,一种是MSSQL。 I tried to find a place to start on Google, but I wasn't successful. 我试图在Google上找到一个起点,但没有成功。 I didn't found anything useful on previous questions similar to mine on StackOverflow. 在以前的问题上,我没有发现任何有用的东西,类似于StackOverflow上的我的问题。

Did any of you connected CI app with MySQL and MSSQL ? 你们中的任何人都将CI应用程序与MySQL和MSSQL连接了吗?

you can put your db config on application/config/database.php like this example: 您可以将db config放在application / config / database.php上,例如以下示例:

$active_group = "default";
$active_record = TRUE;

/*MYSQL DB config EXMPALE */
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'pass';
$db['default']['database'] = 'DATABASE_NAME';
$db['default']['dbdriver'] = 'mysqli';
//...


/* MSSQL DB config EXMPALE, note the first param my_mssql */

$db['my_mssql']['hostname'] = 'SQL SERVER IP';
$db['my_mssql']['username'] = 'username';
$db['my_mssql']['password'] = 'pass';
$db['my_mssql']['database'] = 'DATABASE_NAME';
$db['my_mssql']['dbdriver'] = 'mssql';
//...

Note that we made the default group is mysql so if you call $this->db->.. it's will use the default group db. 请注意,我们将默认组设置为mysql,因此,如果您调用$ this-> db-> ..,它将使用默认组db。

for query with another connection ex. 用于与另一个连接ex进行查询。 MSSQL you will add something like this in your model MSSQL,您将在模型中添加类似的内容

class example_model extends CI_Model
{
    var $mssql;
    function __construct()
    {
        parent::__construct();
        $this->mssql = $this->load->database ( 'my_mssql', TRUE );
    }

    function get_some_mssql_rows(){
       //use $this->mssql instead of $this->db
       $query = $this->mssql->query('select * from mssql_table');
       //...
    }

    function get_some_mysql_rows(){
       //use  $this->db for default 
       $query = $this->db->query('select * from mysql_table');
       //...
    }
}

you can use this way for many dbs connections like read replica for example 您可以将这种方式用于许多数据库连接,例如只读副本

You just need to add both database connection details in the "application/config/database.php" file. 您只需要在“ application / config / database.php”文件中添加两个数据库连接详细信息。 Make sure you set the dbdriver field correctly for each, ie "mysql" and "mssql". 确保为每个数据库设置正确的dbdriver字段,即“ mysql”和“ mssql”。

I got it to work using the below setup 我使用以下设置使其工作

In database.php file put the below. 在database.php文件中放入以下内容。 Note replace the place holders for the hostname, username,password and database for both configs. 请注意将占位符替换为两个配置的主机名,用户名,密码和数据库。

active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'xxx.xxx.xxx.xxx',
    'username' => '<username>',
    'password' => '<password>',
    'database' => '<database>',
    'dbdriver' => 'mysqli',
    '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['MSSQL'] = array(
    'hostname' => '<MSSQLHOSTIP\INSTANCE>',
    'username' => '<mssqluser>',
    'password' => '<mssqlpassword>',
    'database' => '<mssqldatabasename>',
    'dbdriver' => 'sqlsrv',
    '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
);

Second config uses MSSQL as group name and this will be used to load the DB in the model class below. 第二个配置使用MSSQL作为组名,这将用于在下面的模型类中加载数据库。 I tried the config with less options and it would not load so I used all the options from the default config 我尝试了使用较少选项的配置,但它不会加载,因此我使用了默认配置中的所有选项

Define a class variable var $mssql and initialize in the constructor using $this->mssql = $this->load->database ( 'MSSQL', TRUE ); 定义一个类变量var $mssql并使用$this->mssql = $this->load->database ( 'MSSQL', TRUE );在构造函数中初始化$this->mssql = $this->load->database ( 'MSSQL', TRUE );

class User_model extends CI_Model
{
    var $mssql;

    function __construct()
    {
        parent::__construct();
         $this->mssql = $this->load->database ( 'MSSQL', TRUE );
    }

    function GetUsers(){

        $query = $this->mssql->query('Select * from [YOURTABLE]');

        $res = $query->result();

        return $res;    
    }
}

The results are returned as an array of objects. 结果以对象数组形式返回。

Hope this helps someone 希望这可以帮助某人

Try This: 尝试这个:

$db['default'] = array(
'dsn'    => 'sqlsrv:server=127.0.0.1,1433;Database=YourDatabaseName',
'hostname' => '',
'username' => '',
'password' => '',
'database' => 'YourDatabaseName',
'dbdriver' => 'sqlsrv',
'save_queries' => TRUE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM