Welcome Guest, Not a member yet? Register   Sign In
Multiple database connection problem
#1

[eluser]Unknown[/eluser]
I am not able to open the multiple database connection using hooks.Here is the method I am using for this.

function loadDatabaseInfo()
{
// set the XML file name as a PHP string
$databaseConfList = getcwd()."/conf/database.xml" ;
// load the XML file
$xml = @simplexml_load_file($databaseConfList) or die ("no file loaded") ;
// assign the listName element to a string

// $xml->databaseGroup[1]->StudioName = "hello1";
// $filedata = $xml->asXML();
//
// file_put_contents($databaseConfList,$filedata);
$i = 0;
foreach ($xml->databaseGroup as $studioList)
{
$config = null;
$config['name'] = $studioList->Name;
$config['hostname'] = $studioList->configuration->Server;
$config['username'] = $studioList->configuration->DBUser;
$config['password'] = $studioList->configuration->DBUser;
$config['database'] = $studioList->configuration->DBPassword;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['active_r'] = TRUE;
$DB[i] = $this->load->database($config);
echo $i++;
}

return null;


Can anybody please suggest how to get this done.
#2

[eluser]xwero[/eluser]
To connect to multiple instances you have to set the second argument of the load to true and you have to rename the object

Code:
oreach ($xml->databaseGroup as $studioList)
{
$config = null;
$config[’name’] = $studioList->Name;
$config[’hostname’] = $studioList->configuration->Server;
$config[’username’] = $studioList->configuration->DBUser;
$config[’password’] = $studioList->configuration->DBUser;
$config[’database’] = $studioList->configuration->DBPassword;
$config[’dbdriver’] = “mysql”;
$config[’dbprefix’] = “”;
$config[’pconnect’] = FALSE;
$config[’db_debug’] = TRUE;
$config[’active_r’] = TRUE;
$DB{i} = $this->load->database($config,true); // changed line
echo $i++;
}

instead of $this->db->query() you have to use $DB1->query().
#3

[eluser]isCode[/eluser]
The following code doesn't work for me. It shows

"An Error Was Encountered
Unable to connect to your database server using the provided settings."

I checked the database setting, its seems okay to me.

$i = 0;
foreach ($xml->databaseGroup as $studioList)
{

$studio = $studioList->Name;
$active_group = '$studio';

$config['$studio']['hostname'] = $studioList->configuration->Server;
$config['$studio']['username'] = $studioList->configuration->DBUser;
$config['$studio']['password'] = $studioList->configuration->DBUser;
$config['$studio']['database'] = $studioList->configuration->DBPassword;
$config['$studio']['dbdriver'] = "mysql";
$config['$studio']['dbprefix'] = "";
$config['$studio']['pconnect'] = FALSE;
$config['$studio']['db_debug'] = TRUE;
$config['$studio']['active_r'] = TRUE;
$config['$studio']['cache_on'] = FALSE;
$config['$studio']['cachedir'] = "";


$this->$DB1 = $this->load->database($config['$studio'],TRUE);
$i++;
}
#4

[eluser]isCode[/eluser]
Problem solved, I was passing wrong value to database configuration.

Now, I want to load all the databases info in the first page then after clicking on a specific database load only that database information.So, from the secend page I don't need the other connections.

In that case, what should be the best place to load the mulitple connection method. Is it from plugin, hocks or to every controller constructor.
#5

[eluser]xwero[/eluser]
That is the way CI uses the database config file. It doesn't load all the databases put you just put all the information you need in the config/database.php file and load the database you want on the pages/in the models you want.

I would suggest you load the databases you want in the models so you don't have to depend on controllers to open the right database connection and i guess it will save you some resources too.




Theme © iAndrew 2016 - Forum software by © MyBB