当前位置:网站首页>通过 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
边栏推荐
- MySQL query library size
- How to write the keywords in the cover and title? As we media, why is there no video playback
- 激活函数的优缺点和选择
- Grep was unable to redirect to the file
- Collation of errors encountered in the use of redis shake
- Async keyword
- The win10 taskbar notification area icon is missing
- nuxt项目:全局获取process.env信息
- MySQL Basics
- The life cycle of key value in redis module programming
猜你喜欢
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Knn,Kmeans和GMM
How to upload large files quickly?
Tencent has written a few words, Ali has written them all for a month
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
asp. Net method of sending mail using mailmessage
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
重定向和请求转发详解
How to design a good API interface?
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
随机推荐
SSH connects to the remote host through the springboard machine
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
js——实现点击复制功能
redis-shake 使用中遇到的错误整理
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
Async void caused the program to crash
8.3 language model and data set
kubernetes之常用Pod控制器的使用
22年了你还不知道文件包含漏洞?
Mysql database explanation (VII)
MySQL InnoDB transaction
群体智能自主作业智慧农场项目启动及实施方案论证会议
Sword finger offer (1) -- for Huawei
Deep learning - Super parameter setting
Adobe Illustrator menu in Chinese and English
How to design a good API interface?
Have you really learned the operation of sequence table?
免费在upic中设置OneDrive或Google Drive作为图床
UML learning_ Day2
Compiling OpenSSL