连接到ms sql 2005从php使用freetds在centos上

问题描述:

我在从PHP连接到MS SQL 2005时出现问题。连接到ms sql 2005从php使用freetds在centos上

我能够从壳连接,使用...

tsql -S 10.0.0.134 -p 1433 -U gareth 

输入一个简单的查询按预期工作...

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) 

但是,从PHP脚本试图这样做不工作...

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

...并产生一个mssql_connect()[function.mssql-connect]:无法连接到服务器错误。

我可以在/ usr/lib/php/modules中看到mssql.so模块,phpinfo()显示模块已加载。

我很乐意使用odbc_connect代替,如果有人能告诉我一个例子配置freetds.conf和odbc.conf

感谢

+1

仅供参考:请不要使用'mysql_ *'函数获取新代码。 他们不再维护,社区已开始[弃用流程](http://goo.gl/KJveJ)。 请参阅[**红框**](http://goo.gl/GPmFd)? 相反,您应该了解[准备语句](http://goo.gl/vn8zQ)并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net) /库MySQLi)。 如果你不能决定,[这篇文章](http://goo.gl/3gqF9)将有助于选择。如果你关心学习,[这是一本很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-10 15:52:37

这是我能找到的关于PHP.net您的问题。也许它会帮助你解决它。

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. 
+0

谢谢!调试非常有帮助,我发现SELinux会导致这个问题。 – garethmurton 2012-07-10 11:26:49

+3

我有同样的问题,这个回复帮了我:谢谢!在我的情况下,我简单地解决了: 'setsebool -P httpd_can_network_connect 1' 这里解释:[comments.gmane.org/gmane.comp.db.tds.freetds/8627](http://comments.gmane.org /gmane.comp.db.tds.freetds/8627) – deste 2013-01-08 08:05:35