php mssql 调用odbc,通过PDO ODBC将PHP连接到MSSQL

这篇博客指导读者如何在Ubuntu 12.04及类似*nix系统上配置连接到Microsoft SQL Server。主要涉及配置文件/etc/odbc.ini、/etc/odbcinst.ini和/etc/freetds/freetds.conf,以及安装unixodbc和freetds软件包。在PHP中使用PDO连接数据库的示例也给出了。确保TDS_Version与MSSQL版本匹配,并在修改配置后重启apache。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小编典典

您需要设置几个配置文件。/etc/odbc.ini,/etc/odbcinst.ini而/etc/freetds/freetds.conf(这些位置是有效的Ubuntu

12.04,并可能是正确的大多数* nixes)。

您需要安装unixodbc和freetds(不确定CentOS上的软件包名称是什么)。在Ubuntu中将是apt-get install

unixodbc tdsodbc。

# 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 Server

Driver = freetds

Database = XXXXXX

ServerName = MSSQL

TDS_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 TDS

Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so

#Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so

UsageCount = 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列为“库版本” ,您将知道它起作用。

2020-05-26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值