当前位置:网站首页>通过 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
边栏推荐
- The wechat applet optimizes the native request through the promise of ES6
- UML学习_day2
- Crawling fragment of a button style on a website
- tcp_ Diag kernel related implementation 1 call hierarchy
- How does eolink help telecommuting
- Have you really learned the operation of sequence table?
- Five data types of redis
- Grep was unable to redirect to the file
- Educational Codeforces Round 127 A-E题解
- Deep learning - Super parameter setting
猜你喜欢
Lotus DB design and Implementation - 1 Basic Concepts
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Advanced version of array simulation queue - ring queue (real queuing)
8.4 realization of recurrent neural network from zero
Basic operation of circular queue (Experiment)
How to design a good API interface?
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
How to use OCR in 5 minutes
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
2022年中国数字科技专题分析
随机推荐
Openstack theoretical knowledge
Five data types of redis
机器学习——逻辑回归
Hj31 word inversion
kubernetes之常用Pod控制器的使用
激活函数的优缺点和选择
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Async keyword
群体智能自主作业智慧农场项目启动及实施方案论证会议
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
API gateway / API gateway (II) - use of Kong - load balancing
Async void caused the program to crash
tcp_ Diag kernel related implementation 1 call hierarchy
Knn,Kmeans和GMM
Mysql database explanation (8)
Precautions for use of dispatching system
Set onedrive or Google drive as a drawing bed in upic for free
adobe illustrator 菜单中英文对照
The win10 taskbar notification area icon is missing
X509 certificate cer format to PEM format