SQL Server DNS

Was having problems getting my PDO to connect to the SQL Server with ODBC.
And to make matters worse I am on a windows LAN, the web server is a *nix box and my testing computer is M$. My DSN just was not right and wouldn’t play – my host string was off and any permutation I used wasn’t working. Looked at the PDO docs, the DSN docs in the manual, connectionstrings.com and no love.

I am most comfortable working with MySQL so getting the SQL Server to play had been frustrating in the extreme (hate when simple things are made hard – wanted to kick something). Turned out I needed to add my “instance” of the SQL server (not my word – bosses) to the host. So what did I finally come up with that works

$host = ‘\InstanceName’;
$user = ‘user’;
$pass = ‘xxxxxxxx’;
$db = ‘test’;

$dbh = new PDO(“odbc:Driver={SQL Server};Server=$host;Database=$db;Uid=$user;Pwd=$pass;”);

* was it just me or was that an alphabet soup?

Comments (18)

kenrickDecember 14th, 2006 at 3:18 pm

if you have freetds compiled in youll have to add the sql server host to your freetds.conf file for it to work. At least thats what I have to do to get any connections, be it odbc or mssql to work.

Wez FurlongDecember 14th, 2006 at 8:32 pm

Not really 100% related, but I’d thought I’d take the opportunity to help an anticipated FAQ… if your web server is on windows then you’ll have a MUCH better time if you set your Driver to SQL Native Client instead of SQL Server. The SQL Native Client driver is far superior to the older SQL Server drivers.

PHPDeveloper.orgDecember 14th, 2006 at 9:53 pm

Ligaya Turmelle’s Blog:…