pear db connections array

Discussion in 'Programming/Scripts' started by aolong, Nov 12, 2008.

  1. aolong

    aolong New Member

    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:

    Code:
    <?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:

    Code:
    //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
     
    Last edited: Nov 12, 2008

Share This Page