say I have a variable 

$id = mt\_rand();

how can I query the mysql database to see if the variable exists in the row id, if it does exist then change the variable $id, once the variable is unique to all other stored ids, then insert it into the database?

Thanks you guys.