Code Igniter中的多个连接数据库

时间:2013-08-21 07:37:35

标签: php mysql sql sql-server codeigniter

我已经在database.php中创建了这个:

   /* user */
$active_group = "user";
$active_record = TRUE;
$db['user']['hostname'] = "jkt";
$db['user']['username'] = "calculation";
$db['user']['password'] = "C4lculation";
$db['user']['database'] = "MedsLi";
$db['user']['dbdriver'] = "sqlsrv";
$db['user']['dbprefix'] = "";
$db['user']['pconnect'] = TRUE;
$db['user']['db_debug'] = TRUE;
$db['user']['cache_on'] = FALSE;
$db['user']['cachedir'] = "";
$db['user']['char_set'] = "utf8";
$db['user']['dbcollat'] = "utf8_general_ci";

$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "maindatabase";
$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";
<br/>'

但$ db ['user'] ['hostname'] =“jkt”中的colomn和table;仍然无法连接。 默认是针对数据库管理员(mysql); user是用户可以访问的数据库(sql server)
错误:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: stdClass::$Comp_Name

    Filename: views/HCM_user_view.php

    Line Number: 36


请帮助
 
这是模型:

$this->db->select('*');
$this->db->limit(100);
$this->db->from('cm');
$query = $this->db->get();
$myRow = $query->row();         
return $query;

查看错误:

foreach($allHCM->result() as $row)
    php echo $row->trans_ref
    php echo $row->Cert_No
    php echo $row->Comp_Name
    php echo $row->status_code

2 个答案:

答案 0 :(得分:0)

如果我能理解你,你应该使用

$db['default']['dbdriver'] = 'sqlsrv';

连接到sqlserver,请看这个问题

CodeIgniter MSSQL connection

答案 1 :(得分:0)

你必须在一个数据库中设置pconnect true,而不是两个......

和... 尝试使用此代码进行多重连接...

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('user', TRUE); 

使用....

$DB1->get('tablename');

或......

$DB2->get('tblname');

不......

$this->db->get('tablename'); // if you use this, this will load the default database....