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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 26, 2011 Share Posted March 26, 2011 Please explain more. Ken Quote Link to comment 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; Quote Link to comment 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.