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.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143

4 Answers4

0

Check server is local or live

if($_SERVER['HTTP_HOST']=='localhost' || $_SERVER['HTTP_HOST']=='127.0.0.1'){
//local datebase
}
else{
// live database
}
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

Use an if condition and compare with the ip's, if it's local than compare with localhost ip's, like 127.0.0.1 or ::1 if IPv6 else use other connection, like

<?php
   if($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1') { 
      // ::1 If loopback address is IPv6
      //Connect Local
   } else {
      //Connect Server
   }
?>
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • thanks.. I got it. I modify your code, because I need remote server at first then local server...... bcoz if two servers are active then remote server will work.... – Md. Shahadat Sarker May 06 '13 at 04:49
  • @Md.ShahadatSarker That's fine :) but I prefer this more, cuz local addresses won't be changing where our server address will change – Mr. Alien May 06 '13 at 04:51
0

Maybe I did not understand you correctly, but you can just create an array of config values, in order of priority, loop over trying to establish a database connection, and jump out of the loop once a connection has been established. For example (using 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;
}
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
-1

Give a try for below code

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

Though I have not used it but might suffice you requirement

Sid
  • 560
  • 7
  • 17
  • i am really going to dv for that deprecated mysqli_* api check this http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя May 06 '13 at 04:45
  • Actually he didn't mention which library is used. so i just mentioned simpler logic. Thanx for updating, will check tht up. – Sid May 06 '13 at 05:29