2010-11-02 63 views
4

我需要在Ubuntu上连接到PHP的MS SQLServer 2008服务,我想这样做使用PDO。我相信我已经安装了所有先决条件库,并且我确实能够在命令行上使用tsql进行连接,并在代码中使用mssql_connect()。我无法弄清楚正确的DSN是什么,或者是否缺少任何其他PDO特定的配置步骤。PHP通过PDO从Linux连接到MS SQLServer 2008

我使用下面的DSN(其中$db*变量填入相应的值):

odbc:Driver=FreeTDS;SERVER=$dbServer;DATABASE=$dbSchema;UID=$dbUser;PWD=$dbPasswd" 

我的错误信息是:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /home/timothy/test.php:4 
Stack trace: 
#0 /home/timothy/test.php(4): PDO->__construct('odbc:Driver=Fre...') 
#1 {main} 
    thrown in /home/timothy/test.php on line 4 

什么额外的配置步骤都我漏掉了?

在此先感谢。

+0

在你的php.ini中设置了什么? 'mssql.secure_connection'如果打开并重新测试。请确保重新启动Apache以及 – 2010-11-02 14:31:27

+0

mssql.secure_connection已关闭。但请记住mssql_ *函数的工作原理,我只是试图使用PDO接口。 – Timothy 2010-11-02 15:20:03

回答

1

如果您可以连接tsql和mssql扩展名,则不需要任何其他配置。你可能只是没有正确的dsn。 This documentation应该有所帮助。尝试new PDO("dblib:host=xx.xx.xx.xx;dbname=mydatabase", "username", "password");

+0

谢谢!这对我有效 – Timothy 2010-11-02 17:46:55