慕工程0101907
您需要设置几个配置文件。/etc/odbc.ini,/etc/odbcinst.ini而/etc/freetds/freetds.conf(这些位置是有效的Ubuntu 12.04,并可能是正确的大多数* nixes)。您需要安装unixodbc和freetds(不确定CentOS上的软件包名称是什么)。在Ubuntu中将是apt-get install unixodbc tdsodbc。如需安装这些软件的帮助,请查看此问题。无法通过Yum Package Manager安装FreeTDS/etc/odbc.ini(此文件可能为空)# Define a connection to a Microsoft SQL server# The Description can be whatever we want it to be.# The Driver value must match what we have defined in /etc/odbcinst.ini# The Database name must be the name of the database this connection will connect to.# The ServerName is the name we defined in /etc/freetds/freetds.conf# The TDS_Version should match what we defined in /etc/freetds/freetds.conf[mssql]Description = MSSQL ServerDriver = freetdsDatabase = XXXXXXServerName = MSSQLTDS_Version = 7.1/etc/odbcinst.ini# Define where to find the driver for the Free TDS connections.# Make sure you use the right driver (32-bit or 64-bit).[freetds]Description = MS SQL database access with Free TDSDriver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so#Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.soSetup = /usr/lib/i386-linux-gnu/odbc/libtdsS.soUsageCount = 1/etc/freetds/freetds.conf(或者您可以在/etc/freetds.conf中找到它)# The basics for defining a DSN (Data Source Name)# [data_source_name]# host = # port = # tds version = # Define a connection to the Microsoft SQL Server[mssql] host = XXXXXX port = 1433 tds version = 7.1您可能必须tds version = 7.1根据您的MSSQL版本更改上面的行。进行这些更改后,您将必须重新启动apache。在您的PHP代码中,您将如下创建PDO对象:$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");请注意,您的用户名可能需要采用以下格式:domain\username。此外,如果您phpinfo()在页面中执行并搜索“ freetds”,它将显示一个mssql部分,其中freetds列为“库版本” ,您将知道它起作用。