简体   繁体   中英

Making a dynamic connection to database server

I have received a ready virtual flight booking system from one friend to use it and it's based on 2 databases under 1 username and password.

The problem that is my host is generating a unique for each database. The good thing in this system is all the connection scripts are in one file and I have modified most of parts but I stuck on the main part; function change_db.

Here is mysqlexec code first:

<?php
$ini = parse_ini_file("data.ini.php");
$host_rfe    = $ini["host_rfe"];
$host_nav    = $ini["host_nav"];
$rfedatabase = $ini["rfedatabase"];
$navdatabase = $ini["navdatabase"];
$login_db_rfe    = $ini["login_db_rfe"];
$pass_db_rfe     = $ini["pass_db_rfe"];
$login_db_nav    = $ini["login_db_nav"];
$pass_db_nav     = $ini["pass_db_nav"];
$port        = $ini["port"];

// Connecting to Database
if(!($sqlconn=@mysql_connect("$host_rfe:$port",$login_db_rfe,$pass_db_rfe))) {
    echo "<p align=\"center\"><big><img src=\"images/redx.png\"><br/><strong>It wasn't possible to connect to MySQL server. Please, check the configurations.</strong></big></p>";
    exit;
}

// Select Database
if(!($con=@mysql_select_db($rfedatabase,$sqlconn))) {
    echo "<p align=\"center\"><big><img src=\"images/redx.png\"><br/><strong>It wasn't possible to connect to database <i>$rfedatabase</i>. Please, check the configurations.</strong></big></p>";
    exit;
}

/*========================================================================
 Function: change_db
    Usage: This function changes the pointer to another MySQL Database
Arguments:
    $sqlconn - Connection pointer
    $db      - Destnation's database
========================================================================*/
function change_db($sqlconn,$db) {

    global $sqlconn;

    if(!($con=mysql_select_db($db,$sqlconn))) {
        echo "<p align=\"center\"><big><img src=\"images/redx.png\"><br/><strong>It wasn't possible to connect to database <i>$db</i>. Please, check the configurations.</strong></big></p>";
        exit;
    }

    return $con;
}
?>

The system will need to connect always to both database depend on the functions. Initially the connection is with and then it needs to swap to and then may go back to first, so on. ,然后它需要交换到 ,然后可以返回到第一个,依此类推。 To do the database swapping, the system uses the function change_db. Example:

change_db($sqlconn,$navdatabase);
--some functions--
change_db($sqlconn,$rfedatabase);
--some functions--

So here comes the issue with 问题就出现了 . The connection will be always to host and will not switch to the other one. 主机,不会切换到另一个主机。

I have changed with this code but did not work:

if($db == $rfedatabase){
    $sqlconn=mysql_connect("$host_rfe:$port",$login_db_rfe,$pass_db_rfe);
    exit;
} else {
    $sqlconn=mysql_connect("$host_nav:$port",$login_db_nav,$pass_db_nav);
    exit;
}

Any idea how to make the connection relevant to the needed database?

Alright I found the issue, I just forgot to do SQL Pointer to my global variable.

global $rfedatabase, $navdatabase, $host_rfe, $host_nav, $port, $login_db_rfe, $pass_db_rfe, $login_db_nav, $pass_db_nav;

if($db == $rfedatabase){
    $sqlconn=mysql_connect("$host_rfe:$port",$login_db_rfe,$pass_db_rfe);
} else {
    $sqlconn=mysql_connect("$host_nav:$port",$login_db_nav,$pass_db_nav);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM