Enable connection to MSSQL from PHP in Centos 7 with ODBC

This is a one time setup that most of the software developers have less opportunity to get the exposure. Especially who are just continue maintaining the legacy application. Everything is up and ready.

The Problem

When I started maintaining this one legacy system, I was working on it in my own Windows laptop and everything seems pretty fine with no issues of connection to the staging MSSQL server. The staging platform of this legacy system was also running on a Windows PC. The problem happened while I was trying to setup this system to another new staging server which is Centos 7.

ERROR: Call to undefined function odbc_connect()

Apparently, the system was showing an error. Since I have less experience working with MSSQL database, I really have no idea how to handle this at first. After a quick searching on the internet, I found out that this problem may be caused by missing PHP ODBC extension. I checked on the server and it’s true that this extension is not there.

Install PHP ODBC in Centos 7

Firstly, access to your server via terminal using user with root access. To find installed packages and search php_odbc, enter this line:

yum list installed | grep php_odbc

To install php_odbc, enter this line:

yum install php_odbc

After the installation complete, we’ve got to update the odbc configuration with these 2 steps. You’re gonna need to have basic knowledge to use any terminal editor such as vim or nano to update the file.

1. Edit this file /etc/odbc.ini

This file holds the instance that is referred to within the handler (e.g. php) that connects to the database The configuration defines the server it needs to connect to.

[my_mssql_database]
Driver          =       FreeTDS
Description     =       Connection to my_mssql_database
Database        =       Test_Database
Server          =       10.60.70.10

2. Edit this file /etc/odbcinst.ini

This file holds the configuration for the Driver section in odbc.ini.

[FreeTDS]
Description     = TDS connection
Driver          = /usr/lib/odbc/libtdsodbc.so
Setup           = /usr/lib/odbc/libtdsS.so
UsageCount      = 1
FileUsage       = 1
Trace           = Yes
TraceFile       = /tmp/odbcinst_tr

After that, once the configurations updated, we are now ready to test the query from PHP application to MSSQL database.

Conclusion

I hope this short sharing could help anyone solve similar problem. Even though I never had any experience with PHP ODBC installation, never touch terminal with Centos 7 OS, and never use vim to edit files, I’m glad that I take this challenge. The satisfaction feeling when managed to solve this is amazing. If you have any questions, drop in the comment section below and I’m happy to help.

Leave a Reply

Your email address will not be published. Required fields are marked *