Database select based on host

Hello,

I was wondering how I go about choosing the database based on host.

So I might have something like:




switch ($_SERVER['HTTP_HOST'])

{

    case 'mysite.com':

    {

         $db_name = 'mysite';

         $db_pass = 'mysite';

         $db_user = 'mysite';

         break;

    }

    case 'localhost':

    {

         $db_name = 'localdb';

         $db_pass = 'localpass';

         $db_user = 'localuser';

         break;

    }

}



That way when I make any changes to the config files and upload to the development server I don’t have to manually edit the file to make it work.

Thanks!

This wiki article may help you.

If you want to use the application for multiple domains

then use it as




$domains=array();


$server=$_SERVER['HTTP_HOST'];

if(in_array($server,$domains)){

 // set database settings

}else{

  // settings for localhost

}



Here is one more way to setup the application for multiple domains with multiple databases




$domains=array(

      'mysite.com'=>array(

                        'dbuser'=>'',

                        'dbpass'=>'',

                        'dbname'=>'',

                        'dbhost'=>'',// set if you want

                   ),

);


$server=$_SERVER['HTTP_HOST'];

if(in_array($server,$domains)){

 // set database settings

}else{

  // settings for localhost

}



enjoy

Cheers, exactly what I needed.