Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servername question

Status
Not open for further replies.

jefargrafx

Instructor
Joined
May 24, 2001
Messages
273
Location
US
mysql_connect("localhost","username","password");
mysql_select_db("dbname");

is for my test box locally.

but when I upload I have to change the server name to

mysql_connect("148.1.23.12","username","password");
mysql_select_db("dbname");

148.1.23.12 which is the ip address of the server in production.


my question....is there a better way to do this where I won't have to change the connection per server?

like maybe a varible name for the servername?
or better yet have some script sniff out the servername the file is on? localhost, ip, or whereever....

let me know what ya think




jef
 
Sure, you can use a variable in the place of the string literal you use for the server name. You can replace any parameter to any function with a variable.

What I do is put all my configuration options in a separate file as either constants (using define() statements) or as variables (usually using explicit references to $GLOBALS). Then I include the file and use the variables the included script creates.

That way, if I have to test on one machine and run on another, the local configuration files can hold the local configuration data. I just use the variable name in the code, knowing that the configuration include file will initialize the variables to the correct values for each environment.



Want the best answers? Ask the best questions: TANSTAAFL!
 
When you upload your script to the remote server what is the relation of the web server to the MySQL server?

If both run on the same system it will be 'localhost' again and no change is necessary.

If you want to 'sniff' the servername you might end up with the webserver rather than the MySQL server:
$_SERVER['SERVER_NAME'];

I propose to have a configuration file that is included at the top of every script and sets the connection parameters according to the server it's on:
Code:
if ($_SERVER['SERVER_NAME'] == '[URL unfurl="true"]www.mysite.com'){[/URL]
  $mode = 'production';
} else {
  $mode = 'development';
}

# set values according to mode
if ($mode == 'production'){
  $mysqlHost = '255.255.255.255';
} else {
  $mysqlHost = 'localhost';
}

# later connect as
mysql_connect($mysqlHost..... etc.
 
DRJ478 and sleipnir214

thanks for the input.


DRJ478 to answer your question yes mysql and webserver for both test and production and yes I thought like you did

localhost should work for both, two different host both being local to itself...but that didn't work.

I'll look into the varibale idea with local configuration being set outside the file.

thanks again


jef
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top