尝试获取动态数据库时出错

时间:2015-02-08 04:51:39

标签: php codeigniter model

因为我使用了3个不同的数据库,所以我试图获得动态数据库连接,但是当我尝试运行此代码时出现错误:

<?php
class Connection_model extends CI_Model {
    private $sender_db;
    private $sender_host;
    private $sender_user;
    private $sender_pw;
    private $receiver_db;
    private $receiver_host;
    private $receiver_user;
    private $receiver_pw;

    private function define_database() {
        $db['sender'] = array(
                'dsn'   => '',
                'hostname' => $this->sender_host,
                'username' => $this->sender_user,
                'password' => $this->sender_pw,
                'database' => $this->sender_db,
                'dbdriver' => 'mysqli',
                'dbprefix' => '',
                'pconnect' => FALSE,
                'db_debug' => TRUE,
                'cache_on' => FALSE,
                'cachedir' => '',
                'char_set' => 'utf8',
                'dbcollat' => 'utf8_general_ci',
                'swap_pre' => '',
                'autoinit' => TRUE,
                'encrypt' => FALSE,
                'compress' => FALSE,
                'stricton' => FALSE,
                'failover' => array(),
                'save_queries' => TRUE
        );
        return $db['sender'];
    }

    public function establish($target = 'both') {
        if ($target == 'sender') {
            $this->sender_db = $this->session->get_userdata('sender_db');
            $this->sender_host = $this->session->get_userdata('sender_host');
            $this->sender_user = $this->session->get_userdata('sender_user');
            $this->sender_pw = $this->session->get_userdata('sender_pw');

            $otherdb = $this->load->database($this->define_database(), TRUE);
        }
    }

}

错误说:

  

消息:未定义的偏移量:0

我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您需要使用多个数据库,只需在 application / config / database.php中设置多个数组

/*DEFAULT DATABASE*/
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'mylogin';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'database1';
$db['default']['dbdriver'] = 'mysqli';
$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;


/*SECOND DATABASE*/    
$db['seconddb']['hostname'] = 'localhost';
$db['seconddb']['username'] = 'mylogin';
$db['seconddb']['password'] = 'mypassword';
$db['seconddb']['database'] = 'database2';
$db['seconddb']['dbdriver'] = 'mysqli';
$db['seconddb']['dbprefix'] = '';
$db['seconddb']['pconnect'] = TRUE;
$db['seconddb']['db_debug'] = TRUE;
$db['seconddb']['cache_on'] = FALSE;
$db['seconddb']['cachedir'] = '';
$db['seconddb']['char_set'] = 'utf8';
$db['seconddb']['dbcollat'] = 'utf8_general_ci';
$db['seconddb']['swap_pre'] = '';
$db['seconddb']['autoinit'] = TRUE;
$db['seconddb']['stricton'] = FALSE;

然后在你的模型中,

$second_db = $this->load->database('seconddb', TRUE);
$query = $second_db->get('mytable');

更多详情:http://www.codeigniter.com/user_guide/database/connecting.html

答案 1 :(得分:1)

您可以使用

设置变量
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

使用其他名称然后使用$ config并使用

解析模型
$this->load->model('Model_name', '', $config);

(摘自CI文档)