hostname for testing and remote servers

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
PHP_mySQL__Newbie
Forum Commoner
Posts: 29
Joined: Fri Aug 12, 2011 5:40 pm

hostname for testing and remote servers

Post by PHP_mySQL__Newbie »

I have created a few .php webpages using dreamweaver that I am able to run on my localhost. I have a testing server that I use to test my pages before uploading them on my website.
My hostname is: localhost
Database name: WeeklyPlans
Username: user1
Password: password1
All this information is saved in a file saved in a connection folder.

Now when I upload my webpages on the remote server, the settings are different.
Server name (hostname): SQL123
Database name: WeeklyPlans
Username: user1
Password: password1

I have to change the name of the server in the file everytime I upload files. What am I missing here? This is driving me nuts. Can someone please tell me how can I view my webpages on the remote and localhost without changing the hostname?

Thank you.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: hostname for testing and remote servers

Post by Celauran »

If you're defining your database credentials in some type of config file, you can just have one copy on the test server and one copy on the live server. Configuration files tend to not change terribly often. You could even take that a step farther and have a configuration file that only contains database credentials, precluding it from ever needing to be changed.
JohnK_UK
Forum Newbie
Posts: 5
Joined: Wed Jan 11, 2012 12:16 pm

Re: hostname for testing and remote servers

Post by JohnK_UK »

Celauran wrote:You could even take that a step farther and have a configuration file that only contains database credentials, precluding it from ever needing to be changed.
And to make sure you didn't accidentally overwrite the different settings on the two hosts, you can "cloak" the config file in DreamWeaver.

Am alternative approach might be to assign the items that change between servers to php variables and set their values according to the value of the

Code: Select all

if (isset($_SERVER["SERVER_NAME"]) && $_SERVER["SERVER_NAME"] != "localhost")
{
     $hostname = "localhost";
}
else
{
	$hostname = "SQL123";
}
Or something similar. You can then say

Code: Select all

$db = new mysqli($hostname, "user1", "password1", "WeeklyPlans");
and the connection will work on both test and remote servers.
Post Reply