Link to home
Start Free TrialLog in
Avatar of CoreSupport
CoreSupportFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>$myServer = "server";
assuming it has the same username/password are correct:
$myServer = "localhost";
Avatar of CoreSupport

ASKER

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?
Avatar of blue-dev
blue-dev

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.
>>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.
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
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial