====== Connect to Microsoft SQL Server with ODBC and PHP ======
[[https://www.microsoft.com/en-IE/sql-server/sql-server-2017|Microsoft SQL Server ]]is a relational database management system developed by Microsoft.
This article will describe how to set up unixODBC and PHP drivers to connect to MS SQL server.
===== Installing unixODBC =====
[[http://www.unixodbc.org/|unixODBC]] is an open source project that implements the ODBC API
Best way to install unixODBC is from [[https://slackbuilds.org/repository/14.2/libraries/unixODBC/| slackbuilds.org]].
If you have [[https://github.com/sbopkg/sbopkg|sbopkg]] installed this is as easy as executing the below command:
sbopkg -i unixODBC
unixODBC conflicts with libiodbc in Slackware, so if you remove unixODBC, you will have to re-install libiodbc!
===== Installing Microsoft OBDC Drivers =====
The official ODBC Drivers can be downloaded from Microsoft: [[https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server|download link]]
==== Microsoft ODBC Driver 13.1 ====
The version that i've tried and works was the Debian 8 packages
* **msodbcsql** [[https://packages.microsoft.com/debian/8/prod/pool/main/m/msodbcsql/]]
* **mssql-tools** [[https://packages.microsoft.com/debian/8/prod/pool/main/m/mssql-tools/]]
I've installed them by extracting the deb files using 7z and then copying the contents into the same temporary directory to allow me to create a package using makepkg.
===== OBDC Configuration =====
**/etc/odbcinst.ini**
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.6.0
**/etc/odbc.ini**
[mssql]
Driver=ODBC Driver 13 for SQL Server
Server=tcp:YOUR_SERVER_NAME_OR_IP_ADDRESS
Database=YOUR_DATABASE
===== Microsoft Drivers for PHP for SQL Server =====
The drivers are only supported on PHP7+, so the assumption is that you are either on Slackware64-current (>=2018) or you have built and installed PHP7 manually.
The easiest way to install the drivers are to use PECL
pecl install sqlsrv
pecl install pdo_sqlsrv
Alternatively you can get the official drivers from github and build it yourself: [[https://github.com/Microsoft/msphpsql]]
Check the installation using **php -i|grep "sqlsrv support"** you should see something like this:
bash-4.4$ php -i|grep "sqlsrv support"
pdo_sqlsrv support => enabled
sqlsrv support => enabled
{{tag>howtos software database sqlsrv mssql microsoft_sql_server odbc unixodbc author_feri}}