当前位置:网站首页>通过 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
边栏推荐
- Little red book timestamp2 (2022 / 04 / 22)
- Use of common pod controller of kubernetes
- Leetcode学习计划之动态规划入门day3(198,213,740)
- Kubernetes详解(十一)——标签与标签选择器
- PSYNC synchronization of redis source code analysis
- How to design a good API interface?
- Educational Codeforces Round 127 A-E题解
- Detailed explanation of MySQL connection query
- 机器学习——逻辑回归
- TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
猜你喜欢

8.4 realization of recurrent neural network from zero

Sword finger offer (1) -- for Huawei

How to use OCR in 5 minutes

What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it

函数(第一部分)

Introduction to distributed transaction Seata

Reptile exercises (1)

Have you really learned the operation of sequence table?

Kubernetes详解(九)——资源配置清单创建Pod实战

My raspberry PI zero 2W tossing notes record some problems encountered and solutions
随机推荐
如何设计一个良好的API接口?
Tun equipment principle
Detailed explanation of redirection and request forwarding
Advanced version of array simulation queue - ring queue (real queuing)
Squid agent
【thymeleaf】处理空值和使用安全操作符
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Three uses of kprobe
今日睡眠质量记录76分
Set onedrive or Google drive as a drawing bed in upic for free
电脑怎么重装系统后显示器没有信号了
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
Modify the default listening IP of firebase emulators
adobe illustrator 菜单中英文对照
Grep was unable to redirect to the file
Kubernetes详解(十一)——标签与标签选择器
Tun model of flannel principle
Tencent has written a few words, Ali has written them all for a month
Crawling fragment of a button style on a website
C语言超全学习路线(收藏让你少走弯路)