Question : How to open two connections at one time.

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. The databases are on the same server, but have different users.

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 the `Profiles` table, instead of re-writing the many complicated queries.

Here are the functions that connect to the database:

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
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:

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
$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'];

Answer : How to open two connections at one time.

Hi,

I don't think what you want to do is possible, can you have two open connections each to a separate database, yes. Can you use both connections in one query, I don't believe so.

What you could do though, since they are on the same server, and this is the only thing that saves you, create a new user solely for your purposes and grant them permission to the 2 databases you need data from, then connect with that user. In your sql query denote the database in front of the table

so in the below query, replace db1 with the name of the database that your profiles table is in, and replace db2 with the your other database name. Technically that should work as long as the user making the connection has permissions to both databases.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
$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 `db1`.`Profiles` AS `tp` 
    LEFT JOIN `db2`.`sys_admin_ban_list` AS `tbl` ON `tp`.`ID`=`tbl`.`ProfID`
    LEFT JOIN `db2`.`sys_acl_levels_members` AS `tlm` ON `tp`.`ID`=`tlm`.`IDMember` AND `tlm`.`DateStarts` < NOW() AND (`tlm`.`DateExpires`>NOW() || ISNULL(`tlm`.`DateExpires`))  
    LEFT JOIN `db2`.`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'];
Random Solutions  
 
programming4us programming4us