dongliang7545 2010-08-27 12:34
浏览 40
已采纳

添加另一个数据库连接到函数

I have a set of functions that handle the db connection. I want to add another connection here so I can access a different database at the same time as the current one. Any Suggestions?

The reason I'm looking for a solution is because the script I'm using has very complicated Queries and I would like to be able to simply add the database name in front of it, instead of re-writing the many complicated queries.

Here are the functions that connect to the database:

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

Here is an example Query that I don't want to rewrite. I only need to connect to the Profiles Table on a separate database:

$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'];
  • 写回答

1条回答 默认 最新

  • dongtang6681 2010-08-27 12:47
    关注

    You could remodel the class to hold an array of connections, and then add the connection index to each method in your class.

    $db->query(1, ".....");
    $db->connect(1, "localhost", "username", "password");
    

    An sleek method could be introducing a getConnection() method that selects one of the database connections as the current connection, and then executes your query like so:

    $db->getConnection(1)->connect("localhost", "username", "password", "database");
    $db->getConnection(1)->query(".....");
    

    the method would have to look something like this:

    function getConnection($conn)
     {
      $this->useConnection = $conn;
      return $this;
     }
    

    obviously, for this method, every method in your class would have to be made sensitive of the useConnection property.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘