I have lost count of the times I have worked on a website using my local webserver/mySQL database, uploaded it to a remote host (whether live or testing) and found that nothing worked, before realising that I hadn’t updated the database connection properties meaning that the application is still using my local settings.

To get around this problem I now use a PHP driven conditional set of database connection settings, that will work on both my local set-up and the remote host.

A standard dreamweaver connection file e.g. Connections/conndb.php might look something like this:

$hostname_conndb = "localhost";
$database_conndb = "test_db";
$username_conndb = "test_user";
$password_conndb = "test_password";
$conndb = mysql_pconnect($hostname_conndb, $username_conndb,
$password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

Knowing that the remote host is called something like example.com I now construct a conditional state to check the name of the server hosting the site and then sets the connection parameters based on this check e.g.

$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$remote = strpos($requesturi, "example.com");
if ($remote === false)
{
$hostname_conndb = "localhost";
$database_conndb = "test_db";
$username_conndb = "test_user";
$password_conndb = "test_password";
} else
{
$hostname_conndb = "remote-host";
$database_conndb = "remote-db";
$username_conndb = "remote-user";
$password_conndb = "remote-password";
}
$conndb = mysql_pconnect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

Line 1 uses the PHP array that contains details about the server environment to get the Host name and the Requested URI to populate a variable that holds the URL that was requested by the user.

Line 2 checks for the existence of the remote server name (in this case example.com) within the requested URI and sets $remote to the position that this is found. In reality I am not interested in where it is found, only whether it is found.

Line 3 checks to see if the remote server name was found. it is important to use “===” rather than “==”, as if the remote host name is found in the first position of the URL PHP considers this to be position zero (PHP starts most of its numbering from zero rather than one). Finding example.com in the first position of the string would set $remote to zero, and this would cause it to satisfy $remote == false, which is not what we want. Using “===” checks to see if the values are identical rather than equal. As zero is considered to represent false, zero equals false, but is not identical to it.

If it finds that the remote server name is not found within the server variable, it used the conditional statement to set the variables to reflect the local installation of the database, whereas if it is found it uses the else clause to set the remote parameters .

This enables me to set the connection file once, and not worry about changing it or maintaining two unsynchronised copies on the local and remote hosts.

I also use CartWeaver eCommerce software, which maintains its own connection details in a file called application.php and will set variables similar to this:

$cwGlobalSettings->hostname = "localhost";
$cwGlobalSettings->database = "test_db";
$cwGlobalSettings->databaseUsername = "test_user";
$cwGlobalSettings->databasePassword = "test_password";

The exact same approach can also be used in CartWeaver to avoid having to update this file everytime you wannt to transfer it between your local environment and the remote host e.g.

$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$remote = strpos($requesturi, "example.com");
if ($remote === false)
{
$cwGlobalSettings->hostname = "localhost";
$cwGlobalSettings->database = "test_db";
$cwGlobalSettings->databaseUsername = "test_user";
$cwGlobalSettings->databasePassword = "test_password";
} else
{
$cwGlobalSettings->hostname = "remote-host";
$cwGlobalSettings->database = "remote-db";
$cwGlobalSettings->databaseUsername = "remote-user";
$cwGlobalSettings->databasePassword = "remote-password";
}