Home > Article > Backend Development > How to Connect PHP to MSSQL via PDO ODBC?
Question:
Despite having ODBC available as a driver, attempts to establish a connection using new PDO("odbc:..." yield no response, hanging execution. How can PHP be successfully connected to an MSSQL database via PDO ODBC?
Answer:
Connecting PHP to MSSQL via PDO ODBC requires proper configuration of several files:
Configuration Files:
Specific Configuration:
**[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 **[freetds.conf]** [mssql] host = XXXXXX port = 1433 tds version = 7.1
Restarting Apache and PHP Code:
Execute service apache2 restart to restart Apache.
Create the PDO object as follows:
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");
Note:
The above is the detailed content of How to Connect PHP to MSSQL via PDO ODBC?. For more information, please follow other related articles on the PHP Chinese website!