Character Encoding Issue When Fetching Data from MSSQL with php 5.3.10 via PDO ODBC

Recently while working a project for a client, I needed to grab some data for a nav bar from an mssql db via php.

I was able to connect to the db via PDO ODBC and retrieve the data no problem, but when I looped through and printed the results, they looked like so:

nav borked

Looking in the chrome inspector, my first thought was that it was an encoding issue of some sort.

chrome inspector

So I went down the list of usual encoding suspects, but nothing I did had any effect. Lots of google searches turned up stuff about FreeTDS drivers, freetds.conf and such, but since this was happening on a windows box, that sort of thing didn't seem relevant.

At some point, I looked at the table structure and noticed all the garbled columns were set to varchar(MAX).

varchar max table

In line with the finest traditions of the "throw various bits of mud at the wall and see if'n they sticks" school of programming, I tried changing those columns to varchar(2048)...

varchar 2048 table

...et voilĂ !

nav fixed

Once I knew what I was looking for, I was able to find this in the php bug tracker: Bug #54169 Garbage Pointers returned for (n)varchar(max) columns (SQL Server).

Looks like it's been open since early 2011, so hopefully they get it squared away soon!