php remote mssql connection

I have created a php website which is linked to two mssql databases,
i want to keep one of these databases on my local server and put the other onto the webserver

The code below will work fine for the database on the webserver, but what changes do i have to make to this to be able to access the database that is on my local server?

also, is there any changes (security etc) i have to make on my network or to sql server to allow the website to gain access to it?

I'm only a beginner in php so please explain

thanks
<?
$myServer = "server";
$myUser = "user";
$myPass = "pass";
$myDB = "websitedatabase";

$dbhandle = mssql_connect($myServer, $myUser,$myPass) or die("Could not connect to server");
$selected = mssql_select_db($myDB, $dbhandle) or die("Could not open Database");
?>

Open in new window

CoreSupportAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
if the server is running on the default port, you don't need to provide the part number.
Ex 1 documents how to connect depending on your port:
http://us3.php.net/manual/en/function.mssql-connect.php

<?
$myport = "portnumber";
$myServer = "ipaddress,$myport\sqlservername";

$myUser = "user";
$myPass = "pass";
$myDB = "websitedatabase";
 
$dbhandle = mssql_connect($myServer,$myUser,$myPass) or die("Could not connect to server");
$selected = mssql_select_db($myDB, $dbhandle) or die("Could not open Database");
?>
0
 
hieloCommented:
>>$myServer = "server";
assuming it has the same username/password are correct:
$myServer = "localhost";
0
 
CoreSupportAuthor Commented:
sorry i must of confused you somewhere,

the website will be on a webserver along with one database, so i could use localhost to connect to that database, but how to i get connected to the other database which is on a different server than the website?

do i need to use the ip address or port that my server is using?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
blue-devCommented:
You just need to use the domain like you do now.
If you use localhost, it works because windows / linux maps the domain localhost to represent your local machine. If you want to access your server from outside that machine, you need to use the domain.

Example:
$myServer = "www.myserver.com";

This will do the trick. You do not require a port unless mssql is on a different port then usual. Also be sure to check out if the server is allowed to except connections form non-local-scripts. That may be a future issue cause it would simply block your connection then.
0
 
hieloCommented:
>>do i need to use the ip address or port that my server is using?
Yes, AND that remote site must also accept incoming connections from outside. IF your site is hosted with a web-host, find out if they allow incoming db connections from outside. Some of them do not allow this.
0
 
CoreSupportAuthor Commented:
thanks hielo, i will check that with them,

so is this how i should structure the db connection:

<?
$myServer = "ipaddress\sqlservername";
$myport = "portnumber";
$myUser = "user";
$myPass = "pass";
$myDB = "websitedatabase";
 
$dbhandle = mssql_connect($myServer, $myport, $myUser,$myPass) or die("Could not connect to server");
$selected = mssql_select_db($myDB, $dbhandle) or die("Could not open Database");
?>

or how do i add the ip address and port into the connection?

thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.