Welcome Guest, Not a member yet? Register   Sign In
connecting to multiple DB
#1

[eluser]gulan[/eluser]
Hi all.

In my application im using one DB. its configured in config/datatbase.php as follows

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


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



NOw I want to connect to one more DB. for that I have added folliwing in config/database.php

$secondry_group = "second";
$secondry_record = TRUE;


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



and I tried to connect these two DBs using
$db1 = $this->load->database('default', TRUE);
$db2 = $this->load->database('second', TRUE);

First DB is connecting, but not connecting to second DB?? Where Im wrong
plzz help me

Thanks
gulan
#2

[eluser]elvix[/eluser]
you can't have two persistent connections in mysql. turn the one used less often off (false), and it should work.

pconnect = false
#3

[eluser]gulan[/eluser]
It works...Thanks...

I have one more query... I want to take the backup of second DB .
so I created a file controller/backup.php with the following code.

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('second', TRUE);
$this->load->dbutil();
$backup =& $this->dbutil->backup();
$this->load->helper('file');
write_file('../../../Desktop/mybackup.gz', $backup);


but Im getting the backup of the first database ("default")
in codeigniter documentation I found that we have to change all
$this->db->
to
$DB1->

But here is the only place I want to connect and manipulate DB2 so is there any better and simple way to handle it without changing all $this->db-> ????


Thanks
gulan
#4

[eluser]gulan[/eluser]
somebody please help mee....




Theme © iAndrew 2016 - Forum software by © MyBB