Tags
PHP , MySQL
Asked 8 years ago
4 Oct 2016
Views 965
jabber

jabber posted

mysql connection for differ server

i work mostly code at localhost and than upload at server.
so problem arise that i need to modify connection.php file every time to change username/password/database detail. also arise same problem when with working differ server with same script .
connection.php

  mysqlconnect('host2', 'user2', 'password2');
  mysql_select_db('db2'); 


so any solution for it . so it avoid to change connection file every time when we upload from one server to another ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

simple ! you can use $_SERVER['SERVER_NAME'] to find server name and do connection based on it .


switch($_SERVER['SERVER_NAME'])
{
case 'localhost':
  mysqlconnect('localhost', 'user1', 'password1');
  mysql_select_db('db1');
  break;
case 'server1.hostname.com':
  mysqlconnect('mysql.hostname.com', 'root', 'do');
  mysql_select_db('db');
  break;
case 'server2.hostname.com':
  mysqlconnect('mysql.hostname.com', 'root2', 'do');
  mysql_select_db('db2');
  break;
}
Post Answer