Mssql 函数
在线手册:中文 英文
PHP手册

mssql_guid_string

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

mssql_guid_stringConverts a 16 byte binary GUID to a string

说明

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

Converts a 16 byte binary GUID to a string.

参数

binary

A 16 byte binary GUID.

short_format

Whenever to use short format.

返回值

Returns the converted string on success.

范例

Example #1 mssql_guid_string() example

<?php
$binary 
'19555081977808608437941339997619274330352755554827939936';

var_dump(mssql_guid_string($binary));
var_dump(mssql_guid_string($binarytrue));
?>

以上例程会输出:

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


Mssql 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Converts a 16 byte binary GUID to a string

用户评论:

Corvus Corax (19-Feb-2008 03:58)

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:

<?php

   
/**
     * 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=ord(substr($guid,7,1))>>4;
       
// 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;
       
$typefield=ord(substr($guid,8,1))>>4;
       
$type=-1;
        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;
        return
true;

       
    }

$valid_guid='xxxxxxx'.'A'.chr(0xA2).'xxxxxxx';
if (
mssql_is_guid($valid_guid)) $valid_guid=mssql_guid_string($valid_guid);
echo (
$valid_guid);

?>

source:
http://msdn2.microsoft.com/en-us/library/aa446557.aspx
http://en.wikipedia.org/wiki/GUID

lostaircryptic at hotmail dot co dot uk (09-Feb-2007 11:05)

Here is the function that will present the GUID as we commonly know it in PHP.  I designed this function before noticing the one (slightly documented in here).  It has been fully tested and works.

    function presentGUID($string)
    {

      $hex = '';
      $len = strlen($string);

      $bytes = array();
      for ($i = 0; $i < $len; $i++)
      {
        $bytes [$i] = strtoupper(str_pad(dechex(ord($string[$i])), 2, 0, STR_PAD_LEFT));
      }

      $this->swapbyte ($bytes[0], $bytes[3]);
      $this->swapbyte ($bytes[1], $bytes[2]);
      $this->swapbyte ($bytes[4], $bytes[5]);
      $this->swapbyte ($bytes[6], $bytes[7]);

      $hex = "{";
      for ($i = 0; $i < $len; $i++)
      {
        switch ($i)
        {
          case 4:
          case 6:
          case 8:
          case 10:
            $hex .= "-";
        }

        $hex .= $bytes[$i];
      }
      $hex .= "}";

      return $hex;

    }

    function swapbyte(&$value1, &$value2)
    {
      $tmp = $value1;
      $value1 = $value2;
      $value2 = $tmp;
    }

goofy (12-Oct-2006 09:46)

in php5 on w2k server and apache works well:

echo mssql_guid_string($RS['T_ID']); is same string as unique identifier in SQL manager (T_ID) selectet with a standard sql select clause.
im using it in a form to select just this unique dataset to delete like this:
echo
<form name="Delete" action="'.$_SERVER['PHP_SELF'].'" method="post">
<input type="hidden" name="mode" value="del"/>
<input type="hidden" name="T_ID" value="'.mssql_guid_string($RS['T_ID']).'"/>
<input type="submit" value="DEL"/></form>';

there seems to be a matrix like this one running at least this function returns the same value: if you call it like makeuniqueid($RS['T_ID'])

function makeuniqueid($hs)
{
    if (strlen($hs) == 16)
    {
        $hexstring = bin2hex ($hs);
        return $sql_uniqueid=strtoupper(substr( $hexstring, 6,2).substr( $hexstring, 4,2).substr( $hexstring, 2,2).substr( $hexstring, 0,2).'-'.substr( $hexstring, 10,2).substr( $hexstring, 8,2)
               .'-'.substr( $hexstring, 14,2).substr( $hexstring, 12,2).'-'.substr( $hexstring, 16,4).'-'.substr( $hexstring, 20,12));
    }
    else
    {
        $sql_uniqueid = "not valid";
        return $sql_uniqueid;
    }
}

(08-Jul-2005 04:52)

php3 to php4 note

warning!
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

jhorvath at bcn dot hu (19-Mar-2003 03:25)

Using MSSQL2000 and PHP4.3.1 and FreeTDS-0.61 (always compile it with --enable-msdblib --with-tdsver=7.0 for proper working) this function won't give you the strings what you can see in MS's Enterprise Manager for uniqueidentifiers!
And the given strings also useless in SELECTs!
To get the number what you can reuse in later SELECTs, use this:

$q1 = mssql_query("SELECT cast(id as varchar(36)) FROM sales WHERE ...");

where in table 'sales' the column 'id' type is uniqueidentifier.