我在下面有这段代码,据说它会检测到重复的 id 生成并且不会执行。这段代码是否足以防止重复 ID?
//$guid and $alphabet are other sources from which id is generated
$unique = false;
while($unique == false){
$Guid = NewGuid();
ob_start();
echo base_encode($Guid, $alphabet);
$newid = ob_get_contents();
ob_get_clean();
$query = "SELECT * FROM posts WHERE id='$newid'";
$res = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($res) == 0)
$unique = true;
}