将MSSQL和MySQL与CodeIgniter一起使用

将MSSQL和MySQL与CodeIgniter一起使用

本文介绍了将MSSQL和MySQL与CodeIgniter一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的应用程序连接到两个数据库。一种是MySQL,一种是MSSQL。我试图在Google上找到一个起点,但没有成功。在以前的问题上,我没有发现任何有用的东西,类似于StackOverflow上的我的东西。

I am trying to connect my application to two databases. One is MySQL and one is MSSQL. I tried to find a place to start on Google, but I wasn't successful. I didn't found anything useful on previous questions similar to mine on StackOverflow.

你们中的任何人都将CI应用程序与MySQL和MSSQL连接了吗?

Did any of you connected CI app with MySQL and MSSQL ?

推荐答案

您可以将db config放在application / config / database.php上,例如以下示例:

you can put your db config on application/config/database.php like this example:

$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';
//...

请注意,我们将默认组设置为mysql,因此如果您调用
$ this-> db->。
将使用默认组db。

Note that we made the default group is mysql so if you call$this->db->..it's will use the default group db.

用于与另一个连接ex进行查询。 MSSQL,您将在模型中添加类似的内容

for query with another connection ex. MSSQL you will add something like this in your model

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

这篇关于将MSSQL和MySQL与CodeIgniter一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:40