PHP - 使用ADBDB在同一服务器上同时连接到两个数据库 [英] PHP - Connect to two databases simultaneously on same server using ADBDB

查看:208
本文介绍了PHP - 使用ADBDB在同一服务器上同时连接到两个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用PHP 5.3 / ADODB5 / SQL Server 2008在同一服务器上打开与两个模式的连接?这是我的尝试:

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.

我看了几个地方,你可以省略第二个连接字符串中的服务器名称,如下所示: / p>

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);

但是这给我错误(对$ conn打开的记录集不是有效的对象)。

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.

感谢您的帮助。

推荐答案

这是我做的一个快速类,应该允许你使用adoDB连接到3个数据库:

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;
    }

}

将使用此类:

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

这篇关于PHP - 使用ADBDB在同一服务器上同时连接到两个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆