【问题标题】:Codeigniter - multiple database connectionsCodeigniter - 多个数据库连接
【发布时间】:2012-01-06 07:39:18
【问题描述】:

我必须从主数据库中检索 MySQL 数据库信息,然后连接到该数据库,并获取一些记录。

我的意思是持有一个数据库我想加载另一个数据库。

Codeigniter 可以吗?现在我在我的模型中使用以下代码行。

function connectDb($credential)
{

    $config['hostname'] = $credential['server'];
    $config['username'] = $credential['username'];
    $config['password'] = $credential['password'];
    $config['database'] = $credential['database'];
    $config['dbdriver'] = "mysql";
    $config['dbprefix'] = "";
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    $config['cache_on'] = FALSE;
    $config['cachedir'] = "";
    $config['char_set'] = "utf8";
    $config['dbcollat'] = "utf8_general_ci";

    $DB2=$this->load->database($config);

    $DB2->db->select('first_name,last_name');
    $query = $DB2->db->get('person');
    print_r($query);

}

它不工作还有其他方法吗?

【问题讨论】:

  • 如果您的两个架构共享相同的连接配置,您可以使用$this->db->db_select($database2_name); 在它们之间切换。请参阅.. [codeigniter.com/userguide3/database/… 并向下滚动一点。

标签: mysql database codeigniter


【解决方案1】:
default database
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'test',
    '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
);
another db
$db['database2'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'test_2', // Database name
    '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
   );

在你必须调用的模型中

public function getRecords(){
    // Load database
    $db2 = $this->load->database('database2', TRUE);  

   // Select records from 1st database
   $this->db->select('*');
   $query = $this->db->get('record1');
   $result1 = $query->result_array();  

// Select records from 2nd database
   $db2->select('*');
   $query = $db2->get('record2');
   $result2 = $query->result_array();
   $response = array("response1"=>$result1,"response2"=>$result2);
   return $response;
 }

【讨论】:

    【解决方案2】:

    对我来说很好用……

    这是默认数据库:

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'mydatabase',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        '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
    );
    

    在database.php文件底部添加另一个数据库

    $db['second'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'mysecond',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        '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
    );
    

    在 autoload.php 配置文件中

    $autoload['libraries'] = array('database', 'email', 'session');
    

    通过自动加载数据库库,默认数据库可以正常工作 但是第二个数据库通过使用模型中的构造函数加载和连接,并且 控制器...

    <?php
        class Seconddb_model extends CI_Model {
            function __construct(){
                parent::__construct();
                //load our second db and put in $db2
                $this->db2 = $this->load->database('second', TRUE);
            }
    
            public function getsecondUsers(){
                $query = $this->db2->get('members');
                return $query->result(); 
            }
    
        }
    ?>
    

    【讨论】:

    • 您好之后可以配置第二个数据库吗?所以我登陆了我的索引,我得到了一些变量,我想用这些变量来构建另一个数据库,有可能吗?
    【解决方案3】:
    If you need to connect to more than one database simultaneously you can do so as follows:
    
    $DB1 = $this->load->database('group_one', TRUE);
    $DB2 = $this->load->database('group_two', TRUE);
    

    注意:将单词“group_one”和“group_two”更改为您要连接到的特定组名称(或者您可以按照上述说明传递连接值)。

    通过将第二个参数设置为 TRUE(布尔值),函数将返回数据库对象。

    访问https://www.codeigniter.com/userguide3/database/connecting.html了解更多信息。

    【讨论】:

      【解决方案4】:

      最好的方法是使用不同的数据库组。如果您想像往常一样继续使用主数据库 ($this->db),只需关闭辅助数据库的 persistent connexion 配置选项。只有主数据库才能使用持久连接:

      主数据库

      $db['default']['hostname'] = "localhost";
      $db['default']['username'] = "root";
      $db['default']['password'] = "";
      $db['default']['database'] = "database_name";
      $db['default']['dbdriver'] = "mysql";
      $db['default']['dbprefix'] = "";
      $db['default']['pconnect'] = TRUE;
      $db['default']['db_debug'] = FALSE;
      $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;
      

      辅助数据库(注意 pconnect 设置为 false)

      $db['otherdb']['hostname'] = "localhost";
      $db['otherdb']['username'] = "root";
      $db['otherdb']['password'] = "";
      $db['otherdb']['database'] = "other_database_name";
      $db['otherdb']['dbdriver'] = "mysql";
      $db['otherdb']['dbprefix'] = "";
      $db['otherdb']['pconnect'] = FALSE;
      $db['otherdb']['db_debug'] = FALSE;
      $db['otherdb']['cache_on'] = FALSE;
      $db['otherdb']['cachedir'] = "";
      $db['otherdb']['char_set'] = "utf8";
      $db['otherdb']['dbcollat'] = "utf8_general_ci";
      $db['otherdb']['swap_pre'] = "";
      $db['otherdb']['autoinit'] = TRUE;
      $db['otherdb']['stricton'] = FALSE;
      

      然后您可以将辅助数据库用作数据库对象,同时照常使用主数据库:

      // use master dataabse
      $users = $this->db->get('users');
      
      // connect to secondary database
      $otherdb = $this->load->database('otherdb', TRUE);
      $stuff = $otherdb->get('struff');
      $otherdb->insert_batch('users', $users->result_array());
      
      // keep using master database as usual, for example insert stuff from other database
      $this->db->insert_batch('stuff', $stuff->result_array());
      

      【讨论】:

      【解决方案5】:

      使用这个。

      $dsn1 = 'mysql://user:password@localhost/db1';
      $this->db1 = $this->load->database($dsn1, true);     
      
      $dsn2 = 'mysql://user:password@localhost/db2';
      $this->db2= $this->load->database($dsn2, true);     
      
      $dsn3 = 'mysql://user:password@localhost/db3';
      $this->db3= $this->load->database($dsn3, true);   
      

      用法

      $this->db1 ->insert('tablename', $insert_array);
      $this->db2->insert('tablename', $insert_array);
      $this->db3->insert('tablename', $insert_array);
      

      【讨论】:

      • 您好先生,您能用简单的例子解释一下我们在哪里提供用户名、密码、数据库名称,我们可以在模型结构中给出这个连接吗?
      • user=username, password=password,db1=databasename,localhost=hostname
      【解决方案6】:

      查看您的代码时,我唯一发现错误的是您尝试加载第二个数据库时:

      $DB2=$this->load->database($config);
      

      当您要检索数据库对象时,您必须在第二个参数中传递 TRUE

      来自Codeigniter User Guide

      通过将第二个参数设置为 TRUE(布尔值),函数将 返回数据库对象。

      所以,你的代码应该是:

      $DB2=$this->load->database($config, TRUE);
      

      这将使它工作。

      【讨论】:

        【解决方案7】:

        你应该在`application/config/database.php'中提供第二个数据库信息

        通常,您会设置 default 数据库组,如下所示:

        $db['default']['hostname'] = "localhost";
        $db['default']['username'] = "root";
        $db['default']['password'] = "";
        $db['default']['database'] = "database_name";
        $db['default']['dbdriver'] = "mysql";
        $db['default']['dbprefix'] = "";
        $db['default']['pconnect'] = TRUE;
        $db['default']['db_debug'] = FALSE;
        $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;
        

        请注意,登录信息和设置在名为 $db['default'] 的数组中提供。

        然后您可以在新数组中添加另一个数据库 - 我们称之为“otherdb”。

        $db['otherdb']['hostname'] = "localhost";
        $db['otherdb']['username'] = "root";
        $db['otherdb']['password'] = "";
        $db['otherdb']['database'] = "other_database_name";
        $db['otherdb']['dbdriver'] = "mysql";
        $db['otherdb']['dbprefix'] = "";
        $db['otherdb']['pconnect'] = TRUE;
        $db['otherdb']['db_debug'] = FALSE;
        $db['otherdb']['cache_on'] = FALSE;
        $db['otherdb']['cachedir'] = "";
        $db['otherdb']['char_set'] = "utf8";
        $db['otherdb']['dbcollat'] = "utf8_general_ci";
        $db['otherdb']['swap_pre'] = "";
        $db['otherdb']['autoinit'] = TRUE;
        $db['otherdb']['stricton'] = FALSE;
        

        现在,要实际使用第二个数据库,您必须将连接发送到可以在模型中使用的另一个变量:

        function my_model_method()
        {
          $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
        
          $query = $otherdb->select('first_name, last_name')->get('person');
          var_dump($query);
        }
        

        应该这样做。 连接多个数据库的文档可以在这里找到:http://codeigniter.com/user_guide/database/connecting.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-09
          • 1970-01-01
          • 2011-10-03
          • 1970-01-01
          相关资源
          最近更新 更多