ADODB多个数据库

时间:2013-04-03 13:08:43

标签: php mysql database adodb

我正在使用adodb进行数据库操作,在这里我遇到的问题是我无法在我的脚本中添加两个或三个数据库我使用的是3个数据库,但是在这种情况下它无法正常工作需要帮助。 / p>

myadodb连接文件:

<?php
include_once("/adodb/adodb.inc.php");
include_once("/adodb/adodb-exceptions.inc.php");
class ADb {
    function ADb()
    {
        global $dbserver;
        global $dbuser;
        global $dbpass;
        global $database;

        $dbuser = "";
        $dbpass = "";
        $dbserver = "";                
        $database = "";

        $this->conn1 = &ADONewConnection('mysql'); 
        $this->conn1->PConnect($dbserver, $dbuser, $dbpass, $database);
    }                   
    function query($sql)
    {
        try
        {
            $Result = $this->conn1->Execute($sql);
        }
        catch (exception $e)
        {
            echo $e->msg;
        }
    }
}
?>

我为3个数据库连接创建了3个文件

1 个答案:

答案 0 :(得分:2)

这是我创建的一个快速类,它允许您使用adoDB连接到3个数据库:

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