Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Microsoft SQL Server Functions : mssql_guid_string

mssql_guid_string

Converts a 16 byte binary GUID to a string (PHP 4 >= 4.0.7, PHP 5, PECL odbtp:1.1.1-1.1.4)
string mssql_guid_string ( string binary [, int short_format] )

Examples ( Source code ) » mssql_guid_string

<?php
$binary 
'19555081977808608437941339997619274330352755554827939936';

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

The above example will output:

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


    

Code Examples / Notes » mssql_guid_string

jhorvath

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.


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


goofy

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;
}
}


lostaircryptic

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;
   }


Change Language


Follow Navioo On Twitter
mssql_bind
mssql_close
mssql_connect
mssql_data_seek
mssql_execute
mssql_fetch_array
mssql_fetch_assoc
mssql_fetch_batch
mssql_fetch_field
mssql_fetch_object
mssql_fetch_row
mssql_field_length
mssql_field_name
mssql_field_seek
mssql_field_type
mssql_free_result
mssql_free_statement
mssql_get_last_message
mssql_guid_string
mssql_init
mssql_min_error_severity
mssql_min_message_severity
mssql_next_result
mssql_num_fields
mssql_num_rows
mssql_pconnect
mssql_query
mssql_result
mssql_rows_affected
mssql_select_db
eXTReMe Tracker