我在我的CI应用程序中使用Mysql和Oracle。我试图连接它,但我发现我无法查询Oracle数据库。它总是给出表不存在的错误。
我已经将database.php设置为类似这样的
$active_group = 'oracle';
$active_record = true;
$db['oracle']['hostname'] = '10.10.10.1:1521/ocidb';
$db['oracle']['username'] = 'ociuser';
$db['oracle']['password'] = 'ocipass';
$db['oracle']['database'] = 'ocidb';
$db['oracle']['dbdriver'] = 'oci8';
$db['oracle']['dbprefix'] = '';
$db['oracle']['pconnect'] = TRUE;
$db['oracle']['db_debug'] = FALSE;
$db['oracle']['cache_on'] = FALSE;
$db['oracle']['cachedir'] = '';
$db['oracle']['char_set'] = 'utf8';
$db['oracle']['dbcollat'] = 'utf8_general_ci';
$db['oracle']['swap_pre'] = '';
$db['oracle']['autoinit'] = TRUE;
$db['oracle']['stricton'] = FALSE;
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'mysqldb';
$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;

在控制器中,我想查询oracle中的表,所以我将其加载到那里。
function citizen(){
$this->load->database('oracle',true);
$data['someone'] = $this->people_model->getPeople();
$this->load->view('myview',$data);
}

这是people_model
function getPeople(){
return $this->db->get('people')->result();
}

当我运行它时,它会出错
Error Number: 1146
Table 'mysqldb.people' doesn't exist
SELECT * FROM (`people`) WHERE `id` = '21111'

似乎它仍然使查询进入mysql,而表人们在oracle中。我也尝试在模型中而不是在控制器中加载oracle数据库,但结果相同。
在这种情况下,如何查询oracle。任何答案将不胜感激。感谢。
答案 0 :(得分:5)
我使用Codeigniter与oracle和mysql合作。
您使用$this->load->database('oracle',true);
这应该分配给变量,因为您使用了第二个参数true。
像这样
$oracle_db=$this->load->database('oracle',true);//connected with oracle
$mysql_db=$this->load->database('default',true);//connected with mysql
现在你可以将这两个变量用于你的查询。喜欢
$oracle_db->get('people')->result();
或
$mysql_db->get('people')->result();
所以最后你的模型应该是这样的(不要在你的控制器上加载数据库)
function __construct()//model construct function
{
parent::__construct();
$this->oracle_db=$this->load->database('oracle',true);
$this->mysql_db=$this->load->database('default',true);
}
function getPeople(){
return $this->oracle_db->get('people')->result();
}
希望你能理解。确保它与您的oracle db连接。
我的oracle数据库.php是这样的
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = YOUR_IP)(PORT = 1521))
)
(CONNECT_DATA =
(SID = YOUR_SID)
)
)
";
$db['oracle']['hostname'] = $tns;
答案 1 :(得分:1)
您必须加载数据库并且必须使用该对象从数据库进行查询
$oracle = $this->load->database('oracle',true);
$query = $oracle->query("SELECT * FROM people");
并将pconnect标志更改为false
,因为CI在维护与多个数据库的持久连接方面存在问题。
答案 2 :(得分:0)
尝试这样对我有用
$active_group = 'oracle';
$query_builder = TRUE;
$db['oracle'] = array(
'dsn' => '',
'hostname' => '192.168.0.246:1521/orcl',
//'hostname' => 'localhost',
'username' => 's_dev0101',
'password' => 's_dev0101',
'database' => 'testdb',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
'save_queries' => TRUE,
);
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.0.106', //192.168.0.106
//'hostname' => 'localhost',
'username' => 'aaa',
'password' => 'aaa',
'database' => 'ttttt',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
在模型中:
public $db;
function __construct()
{
parent::__construct();
$this->db = $this->load->database('default',true);//connected with mysql
$oracle_db = $this->load->database('oracle',true);//connected with oracle
var_dump($oracle_db);
}