0

I have two database server. ex:

(1) Local Server:

$host = "localhost";
$user = "root";
$pass = "";
$db="edoctor_s";

(2) Remote server
$host = "****.****.net";
$user = "***_***";
$pass = "****";
$db="*****";

if remote server is not found then local server will be active. I want to use them into one php config file.

4

4 回答 4

0

检查服务器是本地的还是现场的

if($_SERVER['HTTP_HOST']=='localhost' || $_SERVER['HTTP_HOST']=='127.0.0.1'){
//local datebase
}
else{
// live database
}
于 2013-05-06T04:37:53.743 回答
0

使用if条件并与 ip 进行比较,如果它是本地的,而不是与 localhost 的 ip 进行比较,127.0.0.1或者::1如果 IPv6 使用其他连接,例如

<?php
   if($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1') { 
      // ::1 If loopback address is IPv6
      //Connect Local
   } else {
      //Connect Server
   }
?>
于 2013-05-06T04:39:06.327 回答
0

也许我没有正确理解您,但是您可以创建一个配置值数组,按优先级顺序,循环尝试建立数据库连接,并在建立连接后跳出循环。例如(使用MySQLi):

$db_config = array(
    // Try this first.
    array(
        'host' => 'xxxxx.xxxxx.xxxx',
        'user' => 'xxxx_xxxx',
        'pass' => 'xxxxx',
        'db' => 'xxxxx_s'
    ),

    // Then this.
    array(
        'host' => 'localhost',
        'user' => 'root',
        'pass' => '',
        'db' => 'edoctor_s'
    )

    // ...
);

$connection = null;

foreach ( $db_config as $config ) {
    $connection = mysqli_connect( $config[ 'host' ], $config[ 'user' ], $config[ 'pass' ], $config[ 'db' ] );

    // No need to keep looping if we have a valid connection.
    if ( ! mysqli_connect_error() )
        break;
    else
        $connection = null;
}
于 2013-05-06T04:45:50.900 回答
-1

试试下面的代码

//check for localhost
if($con = mysql_connect($host_local, $user_local , $pass_local )){
    mysql_select_db($db_local);
}elseif($con = mysql_connect($host_remote, $user_remote , $pass_remote)){   //Remote connection
    mysql_select_db($db_remote);
}

虽然我没用过但可能满足你的要求

于 2013-05-06T04:42:46.357 回答