PHP & SQL Server - field names truncated PHP & SQL Server - field names truncated sql-server sql-server

PHP & SQL Server - field names truncated


Apparently the problem is with ODBC. There's a bug in PHP where column names are truncated at 31 characters, and the only way to fix this is recompiling PHP, changing the array length of name in /ext/odbc/php_odbc_includes.h (usually 32 but apparently it was 16 in your case), but this is not proven to be either safe or unsafe. Go here and here to see more information about this.


The problem is definitely with the Microsoft ODBC drivers version 11, and are fixed in ODBC Driver 13 Preview for SQL Server.

I discovered this as my development machine running ubuntu 14.04 with the Driver 13 Preview installed works fine, but then when I deployed to our production server running RHEL 7 with the Driver 11, all kinds of havoc ensued as column names were truncated at 15 chars.

Microsoft's documentation is lackluster for Linux support, so if you're running ubuntu, then here's the gist of getting it installed.


The permanent solution is to recompile your PHP as suggested in PHP bug threads or try updating to newer PHP version.

You can work around the problem by selectively renaming columns in your select to shorter ones:

$sql = "SELECT measurement_unit AS measure_unit, * FROM measurements WHERE ID=$id";// now in your array you will have new index called "measure_unit"