I need to generate a random number. After I generate the number, I need to check my table to see if that number has been generated before. If the random number is not unique, I'll re-regenerate the number until a unique number is generated.
Here is my code:
$con=mysqli_connect($host,$user,$pass,$database);
$random = rand(1000, 9999);
$result = mysqli_query($con,"SELECT * FROM tablex WHERE email = '.$random .'");
$num_rows = mysqli_stmt_num_rows ($result);
while ($num_rows > 0) {
$random = rand(1000, 9999);
$num_rows = mysql_num_rows($result);
}
$random = rand(1000, 9999);
I need to know if this algorithm is correct - esp. the while loop. Or is there another more efficient way of doing this?
Aucun commentaire:
Enregistrer un commentaire