I'm trying to create a function in PHP where it checks if the id of the user already exists, the id is created with the function rand
, but even though there are very few chances of the same id appearing, chances are still there! so to never have headache with that part, I have to check if the id has already been inserted.
What I'm trying to do is:
Verify that the id already exists: if yes, generate a new id and check again. if not, follow the code.
How to create this the right way?
$id_usuario = rand(1000000, 9999999);
$consultaBanco = mysqli_query($conecta, "SELECT id_usuario FROM site_user WHERE id_usuario = '".$id_usuario."'") or die (mysqli_error($consultaBanco));
$verificaBanco = mysqli_num_rows($consultaBanco);
if ($verificaBanco > 0){
$id_usuario = rand(1000000, 9999999);
return $consultaBanco;
}