Home > Article > Backend Development > How to Connect PHP to MSSQL via PDO ODBC: A Step-by-Step Guide to Driver Setup
Connect PHP to MSSQL via PDO ODBC: Understanding Driver Setup
When attempting to connect to a MSSQL database via PDO using ODBC, you might encounter issues if the necessary configuration files are not properly set up. Let's explore the key aspects of driver setup to resolve this problem.
Prerequisites:
Configuration Files:
Examples:
odbc.ini:
[mssql] Description=MSSQL Server Driver=freetds Database=XXXXXX ServerName=MSSQL TDS_Version=7.1
odbcinst.ini:
[freetds] Description=MS SQL database access with Free TDS Driver=/usr/lib/i386-linux-gnu/odbc/libtdsodbc.so Setup=/usr/lib/i386-linux-gnu/odbc/libtdsS.so UsageCount=1
freetds.conf:
[mssql] host=XXXXXX port=1433 tds version=7.1
PHP Code:
Once configured, connect to the database using PDO:
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser", "$dbpwd");
Note that your username may require a domainusername format. Execute phpinfo() to verify successful connection by searching for "freetds" in the "mssql" section under the "Libraries" section.
The above is the detailed content of How to Connect PHP to MSSQL via PDO ODBC: A Step-by-Step Guide to Driver Setup. For more information, please follow other related articles on the PHP Chinese website!