Generating a Unique ID by using Rand() <p>Trying to create a ajax private chat. But can't seem to generate a unique ID that hasn't been used by another user.</p> <pre><code> $genid = rand(1,999999999999); foreach($genid as &amp;$rand){ $q = mysql_query("SELECT ".$q_pchat." FROM users WHERE pchat_id1='".$rand."'"); $r = mysql_num_rows($q); if($r == 0){ $chatid = $rand; break; } } </code></pre> <p>Get an internal server error 500. I think it's cause its an infinite loop.</p>