Shamrox Posted June 19, 2008 Share Posted June 19, 2008 I have this working query, but if it finds a duplicate number, I'd like it to continue looping thru until if finds a unique number. How would I alter this? $sql = "SELECT pin FROM `pin_users` ORDER BY pin ASC"; $results = mysql_query($sql); while($row = mysql_fetch_array($results, MYSQL_NUM)){ $data[] = $row[0]; $rando = rand(10000, 99999); } if (in_array($rando, $data)) { echo "Got Dup"; }else{ $rando; } Quote Link to comment https://forums.phpfreaks.com/topic/110938-help-altering-random-number-query/ Share on other sites More sharing options...
fenway Posted June 19, 2008 Share Posted June 19, 2008 in_array() is painfully slow, from what I've read. It would be easier to get all of the PINs (no need to sort), put them in a hash, and then run a while loop using rand() until there's no hash collision. Quote Link to comment https://forums.phpfreaks.com/topic/110938-help-altering-random-number-query/#findComment-569381 Share on other sites More sharing options...
Shamrox Posted June 19, 2008 Author Share Posted June 19, 2008 I'm unsure what you mean by put into a hash Quote Link to comment https://forums.phpfreaks.com/topic/110938-help-altering-random-number-query/#findComment-569393 Share on other sites More sharing options...
fenway Posted June 19, 2008 Share Posted June 19, 2008 I'm unsure what you mean by put into a hash Then this is the wrong forum..... moving to php. Quote Link to comment https://forums.phpfreaks.com/topic/110938-help-altering-random-number-query/#findComment-569529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.