2012-10-08 107 views
-1

我必須在php中使用uniqid()生成自動會話ID,但我必須將其存儲在MSSQL數據庫中。但我無法將其轉換爲uniqueidentifier類型。將PHP中隨機生成的隨機ID轉換爲mssql中的uniqueidentifier

<?php 

//set the random id length 
$random_id_length = 10; 

//generate a random id encrypt it and store it in $rnd_id 
$rnd_id = crypt(uniqid(rand(),1)); 

//to remove any slashes that might have come 
$rnd_id = strip_tags(stripslashes($rnd_id)); 

//Removing any . or/and reversing the string 
$rnd_id = str_replace(".","",$rnd_id); 
$rnd_id = strrev(str_replace("/","",$rnd_id)); 

//finally I take the first 10 characters from the $rnd_id 
$rnd_id = substr($rnd_id,0,$random_id_length); 

echo "Random Id: $rnd_id" ; 
echo "<br>"; 

?> 

這是代碼現在我想這$ rnd_id成唯一標識符類型和存儲到MSSQL數據庫

+0

你到目前爲止嘗試過什麼?我們可以看到一些代碼嗎? – acme

+0

好的,謝謝你告訴我們 – Jelmer

回答

0

不要做這種方式。最好讓MSSQL分配一個唯一的ID並在保存表格行後得到這個ID。這樣你就可以確定你的id真的很獨特(在你的db範圍內)。

$res['LAST_INSERT_ID']將保存最後的插入ID。