How can I connect to MS SQL remote domain server with PHP?

Cesar Aracena
Cesar Aracena used Ask the Experts™
on
Hello,

I've been given two servers for a PHP project inside my company (one for PHP and the other for MS SQL) but my local system administrator has never dealt with remote SQL connections (yeah, I know).

I've managed to use his admin session (thru Skype's desktop presentation) and configured the SQL instance to use a fixed port instead of dynamic (to avoid headaches in the future). I've also created an inbound rule in the SQL server's firewall that allows that same TCP port for the domain. I didn't create an inbound UDP rule for the SQL Browser because I'm not using a dynamic port.

Also, the user that I'm using in my PHP script was created under Security -> Logins but within the database, instead under Security -> Logins for the entire SQL instance. It was created as dbreader and dbwriter only.

This is how my PHP connection looks like (the x are IP address and NNNNN is the port):
$servername = "xxx.xxx.xxx.xxx\\INSTANCENAME, NNNNN";
$connectionInfo = array( "Database"=>"MY_DB_NAME", "UID"=>"loginname", "PWD"=>"somepassword" );
$conn = sqlsrv_connect( $servername, $connectionInfo );

if ($conn) {
     echo "yup";
} else {
     echo "nope";
}

Open in new window


What else can I try to make the connection happen?

BTW, both sqlsrv and pdo_sqlsrv extensions are loading fine in PHP.

Both servers are Windows Server 2016 and SQL is also 2016.

Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are you able to connect from SSMS?
What driver do you use for PHP?
What PHP version do you use?
Post the extension= line from your PHP.
What error is returned by connect?

Try to add
die( print_r( sqlsrv_errors(), true));
to else part
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
In my experience with SQL Server 2008 and 2014, you can connect directly (on the standard port) with just the IP address.  Or you can connect using the server/instance name which requires the sqlbrowser service to be running on the server and the UDP port to be opened.  Note that the sqlbrowser service and the sqlserver service do not run on the same port.
Cesar AracenaPHP Enthusiast

Author

Commented:
@pcelba Thanks. I'd turn off that so nobody could, by mistake open my connection test page and see stuff like my username. Turns out the error code points to the user login (code 18456). Connection is being made properly.

@Dave Yeah, I've set up a port in the 20 thousands and the browser is 1434. Thanks ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial