Database connection script

As a developer, you probably use different servers throughout the development process. When you move your application from one server to another, you maybe have to change the database connection script in order for the application to work on the new server.

If you forget to do this, and the client is sitting in front waiting to see their new web application, it can be quite embarrassing. So, use this script to include several servers in your database connection statements and all will be fine.

<?php

/*This file is intended to be able to connect to your MySQL
database no matter where it is situated.
Insert your connection details for your different hosts into the
switch statement below.
You can add more hosts if you need to. */

$host = $_SERVER['HTTP_HOST'];

switch ($host)
{
case 'localhost':
$server = '';
$user = '';
$db = '';
$password = '';
break;

case 'domain of host 1':
$server = '';
$user = '';
$db = '';
$password = '';
break;

case 'domain of host2':
$server = '';
$user = '';
$db = '';
$password = '';
break;

default:
$server = '';
$user = '';
$db = '';
$password = '';
}

$link = mysql_connect($server, $user, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$selected = mysql_select_db($db,$link)
or die("Could not select database");

?>

Leave a Reply

You must be logged in to post a comment.