gpt4 book ai didi

php - ADODB 多数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:14:47 25 4
gpt4 key购买 nike

我正在使用 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 个文件

最佳答案

这是我制作的一个快速类(class),应该允许您使用 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);

关于php - ADODB 多数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15788241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com