I see my idea has been copied
http://www.codingforums.com/showthread.php?t=247091
Use the $_SERVER['HTTP_HOST'] to determine which system your script is running on. That way you don't even need to set anything to true or false, no constants to define etc.
Just upload all your files (including your mysql config file with no changes for once) and it'll just work straight away. It supports various names / domains for each server (handy if you have subdomains) and can be adjusted for mysqli if needed:
PHP Code:
//Require the file
require_once('mysql.php');
//Open connection and select DB
$MySQL = run_mysql($_SERVER['HTTP_HOST']);
//Or
run_mysql($_SERVER['HTTP_HOST']);
//Need to debug this mysql connection? - Set the second parameter to true.
run_mysql($_SERVER['HTTP_HOST'], true);
This is the mysql.php file:
PHP Code:
<?php
function load_mysql()
{
//Set Mysql connections here:
//MySQL Server 1 - Localhost
$Mysql['Name'] = array('127.0.0.1', 'localhost', '<computer_name>', '192.168.0.1');
$Mysql['Host'] = 'localhost';
$Mysql['User'] = 'root';
$Mysql['Pass'] = 'password';
$Mysql['DB'] = 'your_database_name';
$Connections[] = $Mysql;
//MySQL Server 2 - The website
$Mysql['Name'] = array('www.your-site.com', 'yoursite.com', 'sub.your-site.com');
$Mysql['Host'] = 'localhost';
$Mysql['User'] = 'your-site-mysql-user';
$Mysql['Pass'] = 'password2';
$Mysql['DB'] = 'db_name';
$Connections[] = $Mysql;
return $Connections;
}
function get_mysql($Name)
{
$Connections = load_mysql();
foreach($Connections as $Key => $MySql)
{
if (is_array($MySql['Name']))
{
if (($Search = array_search($Name, $MySql['Name'])) !== false)
{
return $MySql;
}
}
else
{
if (strtolower($Name) == strtolower($MySql['Name']))
{
return $MySql;
}
}
}
die("Database connection for $Name not defined / established.");
}
function run_mysql($Name, $Debug = false)
{
$Connection = get_mysql($Name);
If ($Connection != NULL)
{
If ($SQL = @mysql_connect($Connection['Host'], $Connection['User'], $Connection['Pass']))
{
//MySQL Connection Success
$Output = "Connected to $Connection[Host]<br>\n";
If ($Connection['DB'] != '')
{
$Output .= "Database $Connection[DB] supplied<br>\n";
If (@mysql_select_db($Connection['DB']))
{
//Table Selection Success
$Output .= "Selected $Connection[DB]<br>\n";
}
else
{
//Table Selection Failure
$Output .= "Database not selected: " .mysql_error() ."<br>\n";
}
}
else
{
$Output .= "Database not supplied<br>\n";
}
}
else
{
//MySQL Connection Failure
$Output = "Not connected to $Connection[Host]: " .mysql_error() ."<br>\n";
}
}
if ($Debug)
{
print $Output;
}
return $SQL;
}
?>