问题
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.
回答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);
回答2:
I Found this http://phplens.com/lens/adodb/docs-adodb.htm#ex6 (they are initialize AdoConnection by reference). I hope it should help.
来源:https://stackoverflow.com/questions/15670633/php-connect-to-two-databases-simultaneously-on-same-server-using-adbdb