0

I'm trying to get linux, php and MSSQL to play well with together.

I was able to get it to work for the most part - except that any OID (binary) data types are returned as gibberish UTF-8. For example:

string '�"Ή�%�' (length=8)

What I want to be returned is the hex value as seen when selecting from SQL Server Management tools. For example: 0x15C122CE89002595

Details:

Server: CentOS 6.4

PHP Version: 5.5.9

FreeTDS Version : 0.91RC2 (May 14, 2011)

I'm initalizing the connection with php function mssql_connect. I then execute the query with mssql_query and then fetch the results with mssql_fetch_assoc. I have tried this with PDO as well with no success. This makes me think its an issue with the FreeTDS install? Additionally, my coworkers install of SqlSrv on his windows machine returns the binary OID as expected.

Anyone else experience this issue with OID's being returned not as expected?

Thanks in advance.

hank12323
  • 3
  • 1
  • "are returned as gibberish UTF-8" --- they aren't. They are returned as bytes but for some reason **YOU** `var_dump()` them assuming it will be readable. – zerkms Feb 27 '14 at 00:19

1 Answers1

0

You could convert binary into readable hex string using unpack:

$bin = "\x15\xC1\x22\xCE\x89\x00\x25\x95";
$str = unpack('H*', $bin);
var_dump($str[1]); // string(16) "15c122ce89002595"

Your current code "doesn't work" because var_dump has no idea about data formatting you want (and it's only about formatting, nothing else) and output bytes as-is.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Thanks for the reply. I'll give this a try tomorrow. I ran the short example you wrote and that appears to work. Do you know of a way to change the sql query or freetds config so that it returns the string that I expect? The thing that confuses me most is the results retrieved from a WAMP/SqlSrv stack is that the binary is outputted as the readable hex string. I suppose its a result of different platforms/driver. – hank12323 Feb 27 '14 at 00:49
  • @user1494399: "is that the binary is outputted as the readable hex string" --- because you're outputting it as a string. php doesn't care what to output and will output whatever you ask. – zerkms Feb 27 '14 at 00:50