HCProfessionals Posted March 26, 2011 Share Posted March 26, 2011 I need to choose a random number that doesn't currently exist in the database Link to comment https://forums.phpfreaks.com/topic/231795-choose-random-number/ Share on other sites More sharing options...
kenrbnsn Posted March 26, 2011 Share Posted March 26, 2011 Please explain more. Ken Link to comment https://forums.phpfreaks.com/topic/231795-choose-random-number/#findComment-1192619 Share on other sites More sharing options...
mattal999 Posted March 26, 2011 Share Posted March 26, 2011 You really need to explain more, like Ken said. However, I think I know what you want to do: $done = false; // Keep creating new numbers until we've found one that's unique. do { // Generate a random number between 1 and 999,999. $number = rand(1, 999999); // Check if number exists in database. $query = mysql_query("SELECT * FROM table WHERE number = ".$number); if(mysql_num_rows($query) == 0) { $done = true; } } while(!$done); // $number now contains a unique number. echo $number; Link to comment https://forums.phpfreaks.com/topic/231795-choose-random-number/#findComment-1192627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.