garethcl
garethcl

Reputation: 275

connection to ms sql 2005 from php using freetds on centos

I am having a problem connecting to MS SQL 2005 from PHP.

I am able to connect from the shell, using...

tsql -S 10.0.0.134 -p 1433  -U gareth 

Entering a simple query works as expected...

1> SELECT @@VERSION AS MSSQL_VERSION
2> go
MSSQL_VERSION   
Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
Nov 24 2008 13:01:59 
Copyright (c) 1988-2005 Microsoft Corporation
Express Edition on Windows NT 6.1 (Build 7601: Service Pack 1)

However, trying this from a PHP script does not work...

$test = mssql_connect('10.0.0.134:1433', 'gareth', 'mypass');

... and produces an mssql_connect() [function.mssql-connect]: Unable to connect to server error.

I can see the mssql.so module in /usr/lib/php/modules and phpinfo() shows the module is loaded.

I'd be happy to use odbc_connect instead if someone could show me an example configuration for freetds.conf and odbc.conf

Thanks

Upvotes: 4

Views: 3860

Answers (1)

Bas Wildeboer
Bas Wildeboer

Reputation: 580

Here is what i could find on PHP.net about your problem. Maybe it will help you solve it.

This might be obvious to some, but here is a quick tidbit that might save you some time if you are using FreeTDS in Linux:
Be sure that you have these two lines in freetds.conf:
dump file = /tmp/freetds.log
dump file append = yes

so you can tail -f it in the background of debugging the problem.  This helped me find my issue on on CentOS Linux: 

1) tsql test works

2) php-mssql connection in php also works WHEN RUN FROM THE SHELL

3) running PHP through apache does NOT work.

my /tmp/freetds.log file told me: 
net.c:168:Connecting to MYDBSERVER port MYDBPORT
net.c:237:tds_open_socket: MYDBSERVER:MYDBPORT: Permission denied

and the answer was my firewall/SELinux was denying the Apache processes access to connect to the remote MSSQL DB port, but my shell accounts were fine.

Upvotes: 5

Related Questions