1

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

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 个文件

4

1 回答 1

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);
于 2013-04-03T22:59:53.053 回答