Can we use two database in same view in Codeigniter?

故事扮演 提交于 2020-01-25 06:22:58

问题


I want to access data from two different database.

I want to print the records from two database into same view.

Now my first query is from the another database then the second query is from default database. So I get an error that SecondDatabase.tablenmae doesn't exist and actually the table is in default database

So I defined them like this:

$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'biz_prov';
$db['default']['dbdriver'] = 'mysql';
$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;

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

Now In My View I have a row in which I fetch data from default database.

And then in second row I want data from another database so, I load that database:

$this->db = $this->load->database('bizlms', TRUE);

But I for my first row where I fetch data from the default database I get an error:

Table bizlms.tablname doesn't exist

How to use both database on same view?


回答1:


Try like this, keep db name in one variable and use that variable to fetch the data:

<?php 
$dsn1 = 'mysql://root@localhost/biz_prov'; 
$db1 = $this->load->database($dsn1, true); 
$user_table = $this->session->userdata('user_table'); 
$db1->select($user_table.'.id,display_name,ext,email,auth_user,base_ini_filename,base_ini_id,custom_ini_filename,uc_user,uc_password'); 
$db1->where($user_table.'.site_key',$site_item); 
$db1->join('base_ini','base_ini.id ='.$user_table.'.base_ini_id'); 
$db1->from($user_table); 
$query = $db1->get(); 
$result = $query->result_array(); 
?>



回答2:


Yes, you can use 2 or more database to get info and then display it on a view

application/config/database.php

$active_group = 'default';

$db['default']['hostname'] = "host";
$db['default']['username'] = "username";
$db['default']['password'] = "password";
$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['stats']['hostname'] = "host2";
$db['stats']['username'] = "username";
$db['stats']['password'] = "password";
$db['stats']['database'] = "database2";
$db['stats']['dbdriver'] = "mysqli";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";

controller.php

function do_somthing(){
    $this->load->model(array('model_db1', 'model_db2'));

    $info_db1  = $this->model_db1->get_info1();
    $info_db2  = $this->model_db2->get_info2();

    $data      = array(
                       'info1'  =>  $info_db1,
                       'ifno2'  =>  $info_db2
                      );

    $this->load->view('view', $data);
}

model_db1.php

public function __construct(){
    parent::__construct();

    $database1 = $this->load->database('default', TRUE);
}

function get_ifno1(){
    return $database1->db->get('table')->result();
}

model_db2.php

public function __construct(){
    parent::__construct();

    $database2 = $this->load->database('stats', TRUE);
}

function get_ifno2(){
    return $database2->db->get('table')->result();
}

view.php

<div>
    <?php var_dump($info1)?> 
</div> 
<hr>
<div>
    <?php var_dump($info2)?> 
</div> 


来源:https://stackoverflow.com/questions/36742213/can-we-use-two-database-in-same-view-in-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!