(PHP 4 >= 4.0.7, PHP 5, PECL odbtp >= 1.1.1)

mssql_guid_stringConverts a 16 byte binary GUID to a string


Ta funkcja została USUNIĘTA w PHP 7.0.0.


mssql_guid_string ( string $binary [, bool $short_format = FALSE ] ) : string

Converts a 16 byte binary GUID to a string.



A 16 byte binary GUID.


Whenever to use short format.

Zwracane wartości

Returns the converted string on success.


Przykład #1 mssql_guid_string() example



Powyższy przykład wyświetli:

string(36) "35353931-3035-3138-3937-373830383630"
string(32) "31393535353038313937373830383630"

add a note add a note

User Contributed Notes 3 notes

darylblake at gmail dot com
6 years ago
Since this was removed in PHP7. You can achieve the same output with the following code.

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']);
Corvus Corax
14 years ago
How can a binary GUID be identified as a binary GUID?

- mssql_field_type() returns 'blob' for GUID fields...

- mssql_guid_string() returns strings for all sort of arbitrary input data, regardless of length and content (though not all are necessarily valid GUID string represenations, since length varies)

- is_string() on GUID variables is true

therefore a mssql_is_guid() function would be a nice thing.

Unfortunately, there is very little that makes a GUID a GUID. Aside from the length of 16 bytes there are only 2 indicators (and even those only for microsoft system call compatibly generated GUIDs):

the upper 4 bits of character 7 encode the version of the GUID, and are either 0001 (v1) , 0011 v3), or 0100 (v4)
(ASCII characters 10 - 1f, 30 ('0') to 3f ('?') or 40 ('@') to 4f ('O'))
depending on the encoding version of the GUID.

GUIDs generated by mssql server 2005s "NEWID()" statement are version 4, indicating that the GUID is completely randomly generated

the upper 1 to 3 bits of character 8 which encode the GUID variant. In the case of GUIDs generated by MsSQL server 2005 the variant always seems to be 'Standard' which is indicated by bit sequence 10.
((non)ASCII characters 81 to BF)

so as long as no custom hacked or 3rd party inserted GUIDs are used this should be working:


     * determine wether binary data represents a valid GUID
function mssql_is_guid($guid)
        if (!
is_string($guid)) return false;
        if (
strlen($guid)!=16) return false;
// version 1 : Time-based version Uses timestamp, clock sequence, and MAC network card address
        // version 2 : Reserverd
        // version 3 : Name-based version Constructs values from a name for all sections
        // version 4 : Random version Use random numbers for all sections
if ($version<1 || $version>4) return false;
        if ((
$typefield & bindec(1000))==bindec(0000)) $type=0; // type 0 indicated by 0??? Reserved for NCS (Network Computing System) backward compatibility
if (($typefield & bindec(1100))==bindec(1000)) $type=2; // type 2 indicated by 10?? Standard format
if (($typefield & bindec(1110))==bindec(1100)) $type=6; // type 6 indicated by 110? Reserved for Microsoft Corporation backward compatibility
if (($typefield & bindec(1110))==bindec(1110)) $type=7; // type 7 indicated by 111? Reserved for future definition
        // assuming Standard type for SQL GUIDs
if ($type!=2) return false;


if (
mssql_is_guid($valid_guid)) $valid_guid=mssql_guid_string($valid_guid);
echo (


17 years ago
php3 to php4 note

php4 handles MSSQL GUID like binaries values and not like a string as php3 used to do.
Even if you set  in your php.ini :
mssql.compatability_mode = On
To Top