2

Is it possible to open connections to two schemas on the same server using PHP 5.3/ADODB5/SQL Server 2008? Here's what I'm trying:

//  Connect to users database
$connUsers = NewADOConnection('mssql');
$connUsers-> Connect($server, $user, $password, $dbNameUsers);
$connUsers->SetFetchMode(ADODB_FETCH_ASSOC);    

//  Connect to main database    
$conn = NewADOConnection('mssql');
$conn-> Connect($server, $user, $password, $dbNameMain);
$conn->SetFetchMode(ADODB_FETCH_ASSOC); 

Either one works alone, but queries fail if both are open at the same time. Note that everything is the same except the database name.

A couple of places I looked said that you can omit the server name in the second connection string, like this:

$conn-> Connect(false, $user, $password, $dbNameMain);

But this gave me errors (recordsets opened against the $conn are not valid objects).

I can open and close different connections as I need them, but for maintainability I'd sure like to set all my connections at the top of my scripts and then close them all at the bottom.

Thanks for any help.

Stanton
  • 1,334
  • 4
  • 18
  • 32
  • It's been a while since I used ADOdb, but the FAQ contains an answer for multiple connections that may describe your problem; http://adodb.sourceforge.net/adodb-faq.html – thaJeztah Mar 27 '13 at 22:39
  • Yep, I looked there too before I posted. Nothing I could use. Thanks for your reply, though. – Stanton Mar 27 '13 at 22:54

2 Answers2

1

This is a quick class that I made that should allow you to connect to 3 databases using adoDB:

class Data {
    private static $_dbOne = null;
    private static $_dbTwo = null;
    private static $_dbThree = null;

    protected function __construct() {
    }

    /**
     * This function returns the database connection object
     * @return Object Database Connection
     */
    public static function dbOne() {
        include_once(LIBRARY_PATH.'adodb5/adodb.inc.php');
        if (null === self::$_dbOne) {


            $_connOne = 'mysql://username:password@www.server.com/database';

            self::$_dbOne = &ADONewConnection($_connOne);
            if (self::$_dbOne==false) { die('Could not connect to the database.'); }

        }

        return self::$_dbOne;
    }

    /**
     * This function returns the database connection object
     * @return Object Database Connection
     */
    public static function dbTwo() {
        include_once(LIBRARY_PATH.'adodb5/adodb.inc.php');
        if (null === self::$_dbTwo) {


            $_connTwo = 'mysql://username:password@www.server.com/database';

            self::$_dbTwo = &ADONewConnection($_connTwo);
            if (self::$_dbTwo==false) { die('Could not connect to the database.'); }

        }

        return self::$_dbTwo;
    }

}

    /**
     * This function returns the database connection object
     * @return Object Database Connection
     */
    public static function dbThree() {
        include_once(LIBRARY_PATH.'adodb5/adodb.inc.php');
        if (null === self::$_dbThree) {


            $_connThree = 'mysql://username:password@www.server.com/database';

            self::$_dbThree = &ADONewConnection($_connThree);
            if (self::$_dbThree==false) { die('Could not connect to the database.'); }

        }

        return self::$_dbThree;
    }

}

Here is an example of how you would use this class:

$sql = "SELECT * FROM *";
$results1 = Data::dbOne()->Execute($sql);
$results2 = Data::dbTwo()->Execute($sql);
$results3 = Data::dbThree()->Execute($sql);
bktz
  • 41
  • 6
0

I Found this http://phplens.com/lens/adodb/docs-adodb.htm#ex6 (they are initialize AdoConnection by reference). I hope it should help.

fntlnz
  • 411
  • 2
  • 6
  • 14
  • Yeah, I did see that when I was looking for an answer, but that's no longer supported. See this thread: http://stackoverflow.com/questions/11777908/strict-standards-only-variables-should-be-assigned-by-reference-php-5-4. Thanks for your reply, though. – Stanton Mar 27 '13 at 22:53