我想从两个不同的数据库访问数据。
我想将两个数据库中的记录打印到同一视图中。
现在我的第一个查询来自另一个数据库,然后第二个查询来自默认数据库。所以我得到一个错误,SecondDatabase.tablenmae不存在,实际上该表在默认数据库中
所以我这样定义了它们:
$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;
现在在我的视图中,我有一行从默认数据库中获取数据。
然后在第二行我想要来自另一个数据库的数据,所以我加载了该数据库:
$this->db = $this->load->database('bizlms', TRUE);
但是我在第一行从默认数据库中获取数据时出现错误:
表bizlms.tablname不存在
如何在同一视图上使用这两个数据库?
答案 0 :(得分:3)
是的,您可以使用2个或更多数据库获取信息,然后在视图中显示
应用/配置/ 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>
答案 1 :(得分:1)
尝试这样,将db name保留在一个变量中,并使用该变量来获取数据:
[TestMethod]
public void Factory_Should_Get_Claims_Principal() {
//Arrange
var fakeIdentity = new ClaimsIdentity();
var mockUserClaimsPrincipal = new Mock<IClaimsPrincipalFactory>();
mockUserClaimsPrincipal.Setup(ucp => ucp.GetClaimsPrincipal().Identity).Returns(fakeIdentity);
IClaimsPrincipalFactory userClaimsPrincipal = mockUserClaimsPrincipal.Object;
System.Security.Claims.ClaimsIdentity claimIdentity = userClaimsPrincipal.GetClaimsPrincipal().Identity as System.Security.Claims.ClaimsIdentity;
//Act
//...other code that uses the claimIdentity
//Assert
//...other assertions
}