Welcome Guest, Not a member yet? Register   Sign In
How to resolv mutil database connect which is diffenet db type?
#1

[eluser]chmod[/eluser]
when i use mysql connection in autoload.php,which is below:
$autoload['libraries'] = array('database','session','validation','email','ajax','mysmarty');


and ,the database.php is here:
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "demo";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";


when i change the connection to ms sql server 2005,the connect really is mysql.

$config['hostname'] = "192.168.1.134";
$config['username'] = "liujun";
$config['password'] = "mypassword";
$config['database'] = "demo";
$config['dbdriver'] = "mssql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['active_r'] = TRUE;
$this->load->database($config,TRUE);
$query = $this->db->get('baseinfo');
if ($query->num_rows() > 0){
foreach ($query->result() as $row){
echo $row->password;
}
}

the return is:
can't find the database,because it is still connect to mysql.
how can I resolve this problem?



[Mod edit: removed annoying all caps from the thread title]
#2

[eluser]chmod[/eluser]
I use google and CI's forum/wiki search,all of the replay are not good.
Have a master to do it?
#3

[eluser]Pascal Kriete[/eluser]
By passing in the TRUE you're telling CI to return the database object. $this->db will still reference the old version.

Code:
$msdb = $this->load->database($config,TRUE);
$query = $msdb->get(’baseinfo’);
if ($query->num_rows() > 0)
    {
    foreach ($query->result() as $row)
    {
        echo $row->password;
    }
}
Information on connecting to several databases is at the bottom of this page.

And just out of curiosity, why do you have two vastly different databases?
#4

[eluser]chmod[/eluser]
[quote author="inparo" date="1210281436"]By passing in the TRUE you're telling CI to return the database object. $this->db will still reference the old version.

Code:
$msdb = $this->load->database($config,TRUE);
$query = $msdb->get(’baseinfo’);
if ($query->num_rows() > 0)
    {
    foreach ($query->result() as $row)
    {
        echo $row->password;
    }
}
Information on connecting to several databases is at the bottom of this page.

And just out of curiosity, why do you have two vastly different databases?[/quote]



why the database is vastly different?

i am making the web-service which is using SOAP for payment API.
as the differert company using different database.so
the web service server will check the db type.
if the db type is ms sql server,i will use mssql driver to call the
stored process .and the stored process will return the result to
soap client.then output to users and loging in mysql database;

if the db type is mysql ,i will use mysql driver to call the
stored process.and the stored process will return the result to
soap client.then output to users and loging in mysql database;

and so on .
#5

[eluser]Pascal Kriete[/eluser]
Ok, gotcha. It's just something I've never seen done. Smile. Did you get it to work though?
#6

[eluser]chmod[/eluser]
no,there is not a good idea to resolve it.
#7

[eluser]Pascal Kriete[/eluser]
Did you try the method I outlined, and the userguide page I linked to? What happens?

I want to help you work this out, but I'll need more feedback.
#8

[eluser]Unknown[/eluser]
Hello,

i'm probably late in my answer, but it may help someone else.

I use in a website an odbc connection to one database and about 20 mysql databases.

What I did is to make my default connection to the odbc one i need, and in a model i set "dynamicly" the others connections i use (I use at least 3 connections at the same time) like this :

function bdd($bd)
{
$config['hostname'] = "My hostname";
$config['username'] = "my user";
$config['password'] = "my pass";
$config['database'] = $bd;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
return $this->load->database($config, TRUE);
}

In any of my other function i call it like this :

$db1 = $this->bdd('database1');
$res = $db1->query($query);

$db2 = $this->bdd('database2');
$res2 = $db2->query($query2);

And to use the default database, i use $this->db->query....

Sorry for my poor english and hope it helps.


CodeIgniter 1.6.3 (works with 1.5.4 too)




Theme © iAndrew 2016 - Forum software by © MyBB