当前位置:网站首页>Connect PHP to MSSQL via PDO ODBC
Connect PHP to MSSQL via PDO ODBC
2022-04-23 15:26:00 【allway2】
You need to set up several profiles ./etc/odbc.ini
,/etc/odbcinst.ini
and /etc/freetds/freetds.conf
( These positions are right Ubuntu 12.04 It works , And probably for most *nixes That's right. ).
You need to install unixodbc
and freetds
( Not sure CentOS What is the name of the package on ). stay Ubuntu in , This will be apt-get install unixodbc tdsodbc
.
Help on installing these , Please check this question Can't Install FreeTDS via Yum Package Manager
/etc/odbc.ini( This file may be empty )
# 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( Or you can go to /etc/freetds.conf Find it )
# 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
You may need tds version = 7.1
According to your MSSQL Version change the line above .
After making these changes , You must restart apache.
In your PHP In the code , You will create... Like this PDO object :
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");
Please note that , Your user name may need to be in the following format :domain\username
.
Besides , If you execute and search in the page “freetds”, You will know that it works ,phpinfo()
This will display a mssql part , among freetds List as library version .
The accepted answer is in the actual PHP Is correct in the call . As someone rightly commented , It should call odbc The driver . secondly , It has not been used in odbc.ini The name of the data source configured in (DSN), But it's actually creating a ad-hoc DSN. Instead, :
$pdo = new PDO("odbc:mssql", "$dbuser","$dbpwd");
among mssql refer to odbc.ini Medium DSN object
You can create ad-hoc DSN:
$pd = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
among mssql Now I mean freetds.conf Server object in , and FreeTDS refer to odbcinst.ini Driver object in
( This should really be a comment , But I don't represent ).
If you want to use FreeTDS The driver directly sets pdo odbc Connect To MS SQL The server , Without having to be in the configuration file freetds.conf It is specified in .
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;PORT=$portNo;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
If you have a named instance MSSQL The server , You can delete the port number , And then to server_ip\instance_name Format modification $serverName for example :“192.168.1.1\sqlexpress”, among sqlexpress Is the instance name .
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
Please note that odbcinst.ini Configure driver location in
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = libtdsodbc.so
Setup = libtdsS.so
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231525131265.html
边栏推荐
- Basic operation of circular queue (Experiment)
- Adobe Illustrator menu in Chinese and English
- 如何设计一个良好的API接口?
- TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
- A series of problems about the best time to buy and sell stocks
- Common interview questions of operating system:
- Crawling fragment of a button style on a website
- win10 任务栏通知区图标不见了
- Tencent has written a few words, Ali has written them all for a month
猜你喜欢
How to design a good API interface?
8.4 realization of recurrent neural network from zero
Sword finger offer (2) -- for Huawei
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
Set onedrive or Google drive as a drawing bed in upic for free
Machine learning - logistic regression
How to design a good API interface?
What is the role of the full connection layer?
Byte interview programming question: the minimum number of K
The win10 taskbar notification area icon is missing
随机推荐
Educational codeforces round 127 A-E problem solution
Reptile exercises (1)
Detailed analysis of SQL combat of Niuke database (26-30)
setcontext getcontext makecontext swapcontext
Leetcode学习计划之动态规划入门day3(198,213,740)
Difference between like and regexp
Example of time complexity calculation
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
自主作业智慧农场创新论坛
Will golang share data with fragment append
JUC learning record (2022.4.22)
Llvm - generate local variables
Advanced version of array simulation queue - ring queue (real queuing)
自动化测试框架常见类型▏自动化测试就交给软件测评机构
MySQL Basics
How to use OCR in 5 minutes
How to design a good API interface?
Async void caused the program to crash
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Wechat applet customer service access to send and receive messages