Trying to connect to MS SQL from PHP.....no luck, going insane

update. issue has been solved…work through thread for solution

I’m trying to connect to my Microsoft SQL server (on my machine) through PHP…but it’s not connecting. I’m more of a mysql guy so i’m quite new to this

I have tested the settings with Microsoft SQL Server Management Studio Express and it connected fine.

Here is what i’m doing in php:


$host = 'ComputerName\\ServerName';
$user = 'steve';
$pass = 'awesome';

$connect = mssql_connect("$host", "$user", "$pass");

// and i get....
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server:ComputerName\\ServerName in C:\\xampp\\htdocs\\sitename\\index.php on line 20

I really do not know what else to do. Someone with more experience may be able to shed some light?

edit. Oh yeah, mssql appears to be enabled (looking at my attachment)

Have you enabled TCP connections on your sql server?

no idea. how would i do that?

cheers for quick reply

What version of sql server?

2005

http://support.microsoft.com/kb/914277

hmmmm damn, still didn’t work after enabling TCP

same error: Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: compname\servername in C:\xampp\htdocs\sitename\index.php on line 20

Would help to know what error you are getting.

Also, the mssql_* stuff is really bad, especially with modern versions of Sql Server. I used to generally use ODBC, but MS has also released a sql driver for PHP (or more properly PDO I think) which is really the way to go these days.

error i’m getting:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: compname\servername in C:\xampp\htdocs\sitename\index.php on line 20

did you restart sql after doing the change to tcp stuff?

Well, first dumb thing: can you connect with another tool (say, SS Management Studio Express) using the credentials you supplied.

I’d also repoint towards the new driver as mssql_* is really, really bad.

yeah i restarted. Attached screenshot of TCP enabling

Yep, i pointed that out in my first post that i connected using those credentials

I’m assuming i’ve done it right…view below…

I really need to get this connection going before i can think about new drivers

btw, appreciate the responses, i realise it’s a pretty noobish thread

You also have to go into SQL Server Configuration Manager ->
Sql Server 2005 Network Configuration ->
Protocols For SQL Server
Double click TCP
Change the General->Enabled to YEs
Under IP address change the one with 127.0.0.1 as the ip address to enabled->yes.
then hit ok.
then restart sql.

(This is assuming php and mssql are running from the same box).
also I would also stronly echo wwb_99 with using the new driver for sql server.

Ok i have made those changes, restarted…still no luck. God this is frustrating.

I’m running apache if that makes any difference.

Here is the mssql driver for php

I really need to get this connection going before i can think about new drivers

Not really, it uses a different underlying way to connect and will probably give better error messages, etc. Actually, I’m not even certain if the mssql_* supports named instances, which could be a bit of an issue here.

It’s a pre-existing website that i’m just making a couple modifications too. Need to have a working copy on my machine so i can test what i’m doing…

Ah, that was unclear. Yeah, I guess you gotta run with what they did. Anyhow, have you walked through the discussion on PHP.NET about mssql_connect, it might cover some issues you could be having.

downloaded and installed…must be my luck because still no connection

yeah my bad. If i had it my way i’d be sticking with mysql