在同一应用程序中将Codeigniter连接到mysql和oracle [英] Connecting codeigniter to mysql and oracle in the same application

查看:90
本文介绍了在同一应用程序中将Codeigniter连接到mysql和oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为My CI应用程序使用Mysql和Oracle。我尝试连接它,但发现无法对Oracle数据库进行查询。总是会提示该表不存在。

I'm using Mysql and Oracle for my CI application. I tried to connect it but I found that I cannot make a query to Oracle database. It always gave an error that the table is not exist.

我已经将database.php设置为这样的

I already set the database.php to something like this

$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中查询表,以便将其加载到那里。

In a controller, I want to query a table in oracle so I load it there.

function citizen(){
  $this->load->database('oracle',true);

  $data['someone'] = $this->people_model->getPeople();
  
  $this->load->view('myview',$data);
}

这是people_model

And here is the 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数据库加载到模型中,而不是在控制器中加载,但结果相同。

It seems that it still makes query into the mysql, while the table people is in oracle. I also have tried to load the oracle database in model instead of in controller but same result.

在这种情况下,如何查询oracle。任何答案将不胜感激。谢谢。

How can I make a query to oracle in this case. Any answer would be appreciated. Thanks.

推荐答案

我使用Codeigniter处理了oracle和mysql。

I worked with oracle and mysql using Codeigniter.

您使用了 $ this-> load-> database('oracle',true); ,因为您使用了第二个参数true,所以应将其分配给变量。像这样的

You used $this->load->database('oracle',true); this should be assigned to a variable as you used 2nd parameter true. like this

$oracle_db=$this->load->database('oracle',true);//connected with oracle
$mysql_db=$this->load->database('default',true);//connected with mysql

现在您可以使用这两个变量进行查询了。

Now you can use these two variables for your query.Like

$oracle_db->get('people')->result();

$mysql_db->get('people')->result();

所以最终您的模型应该是这样的(不要在控制器上加载数据库)

So finally your model should be like this(do not load database at your controller)

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数据库连接。

Hope you will understand. Make sure it connects with your oracle db.

我对oracle的database.php就是这样

My database.php for oracle was like this

$tns = "
(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = YOUR_IP)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SID = YOUR_SID)
    )
  )
       ";
$db['oracle']['hostname'] = $tns;

这篇关于在同一应用程序中将Codeigniter连接到mysql和oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆