Hi Guys,

I have a problem using the sqlsrv_connect function (in that it's failing!). I have been using mssql_connect on a server with an older verison of PHP where my connect string worked fine. But putting the information into the sqlsrv_connect doesn't let it connect.

Here's my code that I used on my old script:

$server = "serverIP,serverPort";
$user = "user";
$pass = "password";
$db = "db";

$conn = mssql_connect($server, $user, $pass) or die("Couldn't connect to SQL Server");

And that worked fine. I then use this on the new server:

$server = "serverIP,serverPort";
$user = "user";
$pass = "pass";
$db = "db";
$connInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);

$conn = sqlsrv_connect($server, $connInfo) or die("Couldn't connect to SQL Server");

And it comes up saying it couldn't connect. I'm not sure whether I need to change my server variable maybe? (Although the database is still on the same server, so not sure why that would be different) but everything I've read has said something about using serverName/InstanceName. I'm not sure what this is though.

Any help would be greatly appreciated.

Thanks in advance :)

Recommended Answers

All 2 Replies

Instance name is the name for your running database service (you specified this during install). You should be able to find out what it is with Management Studio.

Add this for more information:

die(print_r(sqlsrv_errors(), true));

Oh my god, I'm so stupid.

Just realised that we changed the passwords for everything yesterday. It's working now. Thanks for that little bit of code to get up more details about the error though. It really helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.