当前位置:网站首页>通过 PDO ODBC 将 PHP 连接到 MSSQL
通过 PDO ODBC 将 PHP 连接到 MSSQL
2022-04-23 15:25:00 【allway2】
您需要设置几个配置文件。/etc/odbc.ini
,/etc/odbcinst.ini
和/etc/freetds/freetds.conf
(这些位置对 Ubuntu 12.04 有效,并且可能对大多数 *nixes 是正确的)。
您需要安装unixodbc
和freetds
(不确定 CentOS 上的软件包名称是什么)。在 Ubuntu 中,这将是apt-get install unixodbc tdsodbc
.
有关安装这些的帮助,请查看此问题 Can't Install FreeTDS via Yum Package Manager
/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 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 = <hostname or IP address>
# port = <port number to connect to - probably 1433>
# tds version = <TDS version to use - probably 8.0>
# 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
.
此外,如果您在页面中执行并搜索“freetds”,您将知道它有效,phpinfo()
这将显示一个 mssql 部分,其中 freetds 列为库版本。
接受的答案在实际的 PHP 调用中是正确的。正如有人正确评论的那样,它应该调用 odbc 驱动程序。其次,它没有使用已在odbc.ini中配置的数据源名称 (DSN),但实际上是在创建一个 ad-hoc DSN。反而:
$pdo = new PDO("odbc:mssql", "$dbuser","$dbpwd");
其中mssql指的是odbc.ini中的 DSN 对象
您可以按如下方式创建 ad-hoc DSN:
$pd = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
其中mssql现在指的是freetds.conf中的服务器对象,而FreeTDS指的是odbcinst.ini中的驱动程序对象
(这真的应该是一个评论,但我没有代表点)。
如果您想使用 FreeTDS 驱动程序直接设置 pdo odbc 连接到 MS SQL 服务器,而无需在配置文件 freetds.conf 中指定它。
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;PORT=$portNo;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
如果您有一个具有命名实例的 MSSQL 服务器,您可以删除端口号,然后以 server_ip\instance_name 格式修改 $serverName 例如:“192.168.1.1\sqlexpress”,其中 sqlexpress 是实例名称。
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
请注意在 odbcinst.ini 中配置驱动程序位置
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = libtdsodbc.so
Setup = libtdsS.so
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124360911
边栏推荐
- Common interview questions of operating system:
- setcontext getcontext makecontext swapcontext
- API gateway / API gateway (II) - use of Kong - load balancing
- Thinkphp5 + data large screen display effect
- API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
- adobe illustrator 菜單中英文對照
- Llvm - generate if else and pH
- 我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
- Analysis of common storage types and FTP active and passive modes
- Async keyword
猜你喜欢
函数(第一部分)
自主作业智慧农场创新论坛
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
Analysis of common storage types and FTP active and passive modes
Kubernetes详解(十一)——标签与标签选择器
Basic operation of circular queue (Experiment)
Five data types of redis
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
T2 icloud calendar cannot be synchronized
For 22 years, you didn't know the file contained vulnerabilities?
随机推荐
8.5 concise implementation of cyclic neural network
Leetcode学习计划之动态规划入门day3(198,213,740)
Llvm - generate for loop
Introduction to distributed transaction Seata
C语言超全学习路线(收藏让你少走弯路)
HJ31 单词倒排
Redis master-slave synchronization
A series of problems about the best time to buy and sell stocks
kubernetes之常用Pod控制器的使用
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
Little red book timestamp2 (2022 / 04 / 22)
2022年中国数字科技专题分析
Application of skiplist in leveldb
js——實現點擊複制功能
Mysql database explanation (8)
Set onedrive or Google drive as a drawing bed in upic for free
8.3 language model and data set
重定向和请求转发详解
JUC learning record (2022.4.22)
fatal error: torch/extension.h: No such file or directory