开发者

PHP MS SQL ID format problem

开发者 https://www.devze.com 2023-02-12 10:49 出处:网络
I\'m drawing records into PHP from a legacy MSSQL database built for/by an ASP.net application. The id\'s in ASP appear as long strings of numbers but when retrieved by PHP are in a form开发者_运维知

I'm drawing records into PHP from a legacy MSSQL database built for/by an ASP.net application.

The id's in ASP appear as long strings of numbers but when retrieved by PHP are in a form开发者_运维知识库 like so:

3;Í}±¯I©ûzƒgŸó


In PHP 7 and up you now have to "tell" PDO to use the string representation of the GUID when returning it as opposed to the default binary representation.

See: https://github.com/php/php-src/pull/2001

Solution:

     /* $db represent the PDO connection */
     $db->setAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, true);


I'm going to wager that the IDs are actually GUIDs -- if so you will want to convert your GUIDs to strings using mssql-guid-string (or if you are using PHP 5.3 or later you will want to use Microsoft's SqlSrv extension, which should convert GUID fields automatically.)


Use this function. It retuns cenverted mssql id. Example: G4G3G2G1-G6G5-G8G7-G9G10-G11G12G13G14G15G16

 function convertBinToMSSQLGuid($binguid)
 {
    $unpacked = unpack('Va/v2b/n2c/Nd', $binguid);
    return sprintf('%08X-%04X-%04X-%04X-%04X%08X', $unpacked['a'], $unpacked['b1'], $unpacked['b2'], $unpacked['c1'], $unpacked['c2'], $unpacked['d']);
 }


set in connection following attribute PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER as true

source https://github.com/php/php-src/pull/2001

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号