我需要从 Ubuntu 上的 PHP 连接到 MS SQLServer 2008 服务,我想使用 PDO 来实现。我相信我已经安装了所有必备库,而且我确实能够在命令行上使用 tsql
并在代码中使用 mssql_connect()
进行连接。我无法弄清楚正确的 DSN 是什么,或者我是否遗漏了任何其他特定于 PDO 的配置步骤。
我正在使用以下 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
我忽略了哪些额外的配置步骤?
提前致谢。
最佳答案
如果可以连接 tsql 和 mssql 扩展,则不需要任何其他配置。您可能只是没有正确的 dsn。 This documentation应该有帮助。试试 new PDO("dblib:host=xx.xx.xx.xx;dbname=mydatabase", "username", "password");
关于PHP 通过 PDO 从 Linux 连接到 MS SQLServer 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4074481/