Php – Ubuntu 16.04 PHP 7 & Microsoft ODBC Driver for SQL Server on Linux – odbc_connect undefined

odbcPHPsql serverubuntu-16.04

I'm new to Linux. I have provisioned a new Ubuntu 16.04 server, added LAMP via Tasksel, and added PhpMyAdmin. I added my web app to the html folder, and ran my MySQL scripts. This part works fine. I have a PHP page that queries a remote SQL Server. This is where my issue is:

I installed the SQL Server ODBC Driver per instructions:

https://msdn.microsoft.com/en-us/library/hh568454(v=sql.110).aspx

udo su 
sh -c 'echo "deb [arch=amd64]
https://apt-mo.trafficmanager.net/repos/mssql-ubuntu-xenial-release/xenial main" > /etc/apt/sources.list.d/mssqlpreview.list' 
sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
apt-get update 
apt-get install msodbcsql unixodbc-dev-utf16

SQL Server ODBC is not listed in PhpInfo().

When I hit the page with the remote SQL Server query, I find an error in my Apache2 error log:

[Tue Nov 01 04:42:52.651772 2016] [:error] [pid 1306] [client ::1:33146]
PHP Fatal error: Uncaught Error: Call to undefined function odbc_connect() in /var/www/html/xxxxxxx.php:67\n
Stack trace:\n#0 {main}\n thrown in /var/www/html/xxxxxxx.php on line 67

What am I missing?

Best Answer

I had the same issue with the repo linked in MSDN.

You would need to install the php-odbc package which needs a library that conflicts with the one from the microsoft repo.

I installed the driver manually with the following commands:

sudo apt-get -y install build-essential
cd /tmp/
wget ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.4.tar.gz
tar xvf unixODBC-2.3.4.tar.gz
cd unixODBC-2.3.4/
./configure --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --sysconfdir=/etc
make
sudo make install

sudo apt-get install openssl libkrb5-3 libc6 e2fsprogs

cd /tmp/
wget http://download.microsoft.com/download/B/C/D/BCDD264C-7517-4B7D-8159-C99FC5535680/RedHat6/msodbcsql-11.0.2270.0.tar.gz
tar xvf msodbcsql-11.0.2270.0.tar.gz
cd msodbcsql-11.0.2270.0/
sudo bash install.sh install --force --accept-license

sudo ln -s /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /usr/lib/x86_64-linux-gnu/libcrypto.so.10
sudo ln -s /lib/x86_64-linux-gnu/libssl.so.1.0.0 /usr/lib/x86_64-linux-gnu/libssl.so.10
sudo ln -s /usr/lib/x86_64-linux-gnu/libodbcinst.so.2.0.0 /usr/lib/x86_64-linux-gnu/libodbcinst.so.1

You can enter your connections now in /etc/odbc.ini and replace IP and password. e.g.:

[test]
Driver = ODBC Driver 11 for SQL Server
Server = 192.168.1.2,1433
Database = TEST

and access with PDO:

$db = new PDO('odbc:test', 'USERNAME' 'PASSWORD');