dq23171 2016-04-20 11:20
浏览 41
已采纳

我们可以在Codeigniter中的同一视图中使用两个数据库吗?

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?

  • 写回答

2条回答 默认 最新

  • douchao0358 2016-04-20 12:03
    关注

    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(); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 电脑鼠标键盘不好使了
  • ¥15 求糕手!用matlab求解目标规划模型🙏
  • ¥15 画出的分类图不对,求解答一下
  • ¥20 springboot和springcloud版本问题
  • ¥15 ps2手柄控制树莓派小车
  • ¥30 C#:vsto powerpoint的外接程序
  • ¥30 stata将do文件代码转化为ado文件
  • ¥15 两个同维数组相比,不同位置、出现重复比无意义,而不同位置、不出现重复比,则有意义。把有意义的两个数组放入新的集合MK中。
  • ¥15 可以远程电脑安装nvm
  • ¥15 写一个可直接调用的函数,将32位有符号数转成另一个无符号的数