Cleaning up database - Duplicate users

I have a slight problem. Like 1/10,000 times I have a page and an iframe hit this bit of code simultaneously…

Basically, it checks if the user id exists in the database, and creates and entry for them. Hitting this simultaneously occurs very rarely and I end up with double entries.

■■■■ high speed non-transactional…


$Result = mysql_fetch_array(mysql_query("SELECT sid, visited FROM users WHERE snid=1 && sid=$Sid;"));
if (!isset ($Result["sid"]{1})) {
    mysql_query("INSERT INTO users (snid, sid, nid) VALUES('1','$Sid','$Nid');");
}

Is there any simple way to clear duplicate entries for a index?

Edit: Never mind… I fixed the problem so the iFrame doesn’t hit the code too. However, I may still have a few duplicate users.

I cannot believe that I even noticed it. That database must have been hit twice within .005ms by two scripts, and that’s probably the only time it happened.