PDA

View Full Version : pear db connections array


aolong
12th November 2008, 17:57
I would like to set up some db connections in an array so I can loop through them later, performing queries to different db's (different servers, too) from a single form. I found the following example on the pear site, but am unable to make it work (I am only learning). I am hoping someone can help by detailing how to modify or complete the example for my purposes. The example is:


<?php

/*
* Connect to one of the possible databases
*
* @throws Example_Datasource_Exception when it can't connect to
* any of the configured databases.
*
* @throws Example_Config_Exception when it can't find databases
* in the configuration.
*/

function connect(Config $conf) {
$dsns =& $conf->searchPath(array('config', 'db'));
if ($dsns === FALSE) throw new Example_Config_Exception(
'Unable to find config/db section in configuration.'
);

$dsns =& $dsns->toArray();

foreach($dsns as $dsn) {
try {
$this->connectDB($dsn);
return;
} catch (Example_Datasource_Exception e) {
// Some warning/logging code recording the failure
// to connect to one of the databases
}
}
throw new Example_Datasource_Exception(
'Unable to connect to any of the configured databases'
);
}


I think I am missing exactly how to create the configuration (include?) with my dns's.

I get as far as creating the array thus:

//create an array from the possible connections
$connection = array('test' => '& DB::connect (' . $dsn_test . ')',
'auth' => '& DB::connect (' . $dsn_auth . ')',
'auth2' => '& DB::connect (' . $dsn_auth2 . ')');

-Andrew