Php 向函数添加另一个db连接
我有一组处理db连接的函数。我想在这里添加另一个连接,以便可以在当前数据库的同时访问另一个数据库。有什么建议吗 我之所以寻找解决方案,是因为我使用的脚本有非常复杂的查询,我希望能够简单地在它前面添加数据库名称,而不是重新编写许多复杂的查询 以下是连接到数据库的函数:Php 向函数添加另一个db连接,php,mysql,Php,Mysql,我有一组处理db连接的函数。我想在这里添加另一个连接,以便可以在当前数据库的同时访问另一个数据库。有什么建议吗 我之所以寻找解决方案,是因为我使用的脚本有非常复杂的查询,我希望能够简单地在它前面添加数据库名称,而不是重新编写许多复杂的查询 以下是连接到数据库的函数: function db(){ $this->host = DATABASE_HOST; $this->port = DATABASE_PORT; $this->socket = DATAB
function db(){
$this->host = DATABASE_HOST;
$this->port = DATABASE_PORT;
$this->socket = DATABASE_SOCK;
$this->dbname = DATABASE_NAME;
$this->user = DATABASE_USER;
$this->password = DATABASE_PASS;
$this->current_arr_type = MYSQL_ASSOC;
// connect to db automatically
if (empty($GLOBALS['bx_db_link'])) {
$this->connect();
$GLOBALS['gl_db_cache'] = array();
$GLOBALS['bx_db_param'] = array();
}
else
$this->link = $GLOBALS['bx_db_link'];
if(empty($GLOBALS['bx_db_param']))
$GLOBALS['bx_db_param'] = new BxDolParams($this);
$this->oParams = &$GLOBALS['bx_db_param'];
}
/**
* connect to database with appointed parameters
*/
function connect()
{
$full_host = $this->host;
$full_host .= $this->port ? ':'.$this->port : '';
$full_host .= $this->socket ? ':'.$this->socket : '';
$this->link = @mysql_pconnect($full_host, $this->user, $this->password);
if (!$this->link)
$this->error('Database connect failed', true);
if (!$this->select_db())
$this->error('Database select failed', true);
$this->res("SET NAMES 'utf8'");
$this->res("SET sql_mode = ''");
$GLOBALS['bx_db_link'] = $this->link;
}
function select_db()
{
return @mysql_select_db($this->dbname, $this->link) or $this->error('Cannot complete query (select_db)');
}
/**
* close mysql connection
*/
function close()
{
mysql_close($this->link);
}
下面是一个我不想重写的示例查询。我只需要连接到单独数据库上的Profiles表:
$sQuery = "
SELECT
`tp`.`ID` as `id`,
`tp`.`NickName` AS `username`,
`tp`.`Headline` AS `headline`,
`tp`.`Sex` AS `sex`,
`tp`.`DateOfBirth` AS `date_of_birth`,
`tp`.`Country` AS `country`,
`tp`.`City` AS `city`,
`tp`.`DescriptionMe` AS `description`,
`tp`.`Email` AS `email`,
DATE_FORMAT(`tp`.`DateReg`, '" . $sDateFormat . "' ) AS `registration`,
DATE_FORMAT(`tp`.`DateLastLogin`, '" . $sDateFormat . "' ) AS `last_login`,
`tp`.`Status` AS `status`,
IF(`tbl`.`Time`='0' OR DATE_ADD(`tbl`.`DateTime`, INTERVAL `tbl`.`Time` HOUR)>NOW(), 1, 0) AS `banned`,
`tl`.`ID` AS `ml_id`,
IF(ISNULL(`tl`.`Name`),'', `tl`.`Name`) AS `ml_name`
" . $sSelectClause . "
FROM `Profiles` AS `tp`
LEFT JOIN `sys_admin_ban_list` AS `tbl` ON `tp`.`ID`=`tbl`.`ProfID`
LEFT JOIN `sys_acl_levels_members` AS `tlm` ON `tp`.`ID`=`tlm`.`IDMember` AND `tlm`.`DateStarts` < NOW() AND (`tlm`.`DateExpires`>NOW() || ISNULL(`tlm`.`DateExpires`))
LEFT JOIN `sys_acl_levels` AS `tl` ON `tlm`.`IDLevel`=`tl`.`ID`
" . $sJoinClause . "
WHERE
1 AND (`tp`.`Couple`=0 OR `tp`.`Couple`>`tp`.`ID`)" . $sWhereClause . "
" . $sGroupClause . "
ORDER BY `tp`.`" . $aParams['view_order'] . "` " . $aParams['view_order_way'] . "
LIMIT " . $aParams['view_start'] . ", " . $aParams['view_per_page'];
您可以重新建模类以保存连接数组,然后将连接索引添加到类中的每个方法
$db->query(1, ".....");
$db->connect(1, "localhost", "username", "password");
一个圆滑的方法可能是引入一个getConnection方法,该方法选择一个数据库连接作为当前连接,然后执行查询,如下所示:
$db->getConnection(1)->connect("localhost", "username", "password", "database");
$db->getConnection(1)->query(".....");
该方法必须如下所示:
function getConnection($conn)
{
$this->useConnection = $conn;
return $this;
}
显然,对于这个方法,类中的每个方法都必须对useConnection属性敏感。如果它在类中,因为这是唯一可以这样做的方法,为什么仍然使用全局变量而不是成员变量或静态类变量?这些变量在脚本的不同区域中使用。我没有写它,我只是尝试使用同一个Profiles表集成两个不同的站点。我个人更希望每个连接看到一个实例,并使getConnection成为静态的,但您的方式也应该可以,调试可能会更有趣一些,因为现在您将状态存储在DB对象中,但这应该不会太糟糕……我试图解决的问题是,通过简单地添加数据库,就可以使用脚本中的左连接和所有复杂查询。在表名前面。等等,是指同一服务器上的不同数据库吗?如果是这样,使用一个连接,只需在表前面加上database.table…好的,表中有不同的用户,这会有问题吗?那么,如果当前连接登录的用户没有访问权限,则需要使用不同的连接。。。