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条)

报告相同问题?

悬赏问题

  • ¥15 Django的安装调试运行
  • ¥15 带序列特征的多输出预测模型
  • ¥15 VB.NET读取电脑主板序列号
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取