NLCJ Posted August 14, 2010 Share Posted August 14, 2010 Hello, I'm currently creating a little script, and want to assign a random number to a row in the MySQL table as soon as it will be inserted. But that number may not have been used before. How will I do this? I tried if else thingies, but then the code would be very long (just in case it randomly picks 10 times after each other a number that exists). $randomnumber = rand(10000000, 99999999); if(mysql_num_rows(mysql_query("SELECT * FROM table WHERE randomnumber='".$randomnumber."')) == 1) { $randomnumber = rand(10000000, 99999999); if(mysql_num_rows(mysql_query("SELECT * FROM table WHERE randomnumber='".$randomnumber."')) == 1) { $randomnumber = rand(10000000, 99999999); }else { // Insert into database } }else { // Insert into database } I hope you understand my problem. Regards, Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/ Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 You will be better off grabbing all current random numbers first and putting them into an array. $sql = 'SELECT randomnumber FROM table'; $result = mysql_query($sql); $randomNumbers = array(); while(list($number) = mysq_fetch_row($result)) $randomNumbers[] = $number; Now we'll create a (recursive) function for generating the next random number, we'll pass the $randomNumbers array to this function. Within this function we'll generate a new random number and see if it is already in the $randomNumber array. If the number has not been picked we'll return the new number, otherwise we'll call the function again until a unique random number has been generated. function randNumber(&$randomNumbers) { // generate the new number $newNumber = rand(10000000, 99999999); // check to see if this number has been picked before if(in_array($newNumber, $randomNumbers)) { // this number has already been picked, call the function again return randNumber($randomNumbers); } // number is unique, return this number else { return $newNumber; } } // call the function $number = randNumber($randNumbers); Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099166 Share on other sites More sharing options...
NLCJ Posted August 14, 2010 Author Share Posted August 14, 2010 Smart! I will work on it for a while and let you know how it works out! Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099171 Share on other sites More sharing options...
NLCJ Posted August 14, 2010 Author Share Posted August 14, 2010 Sadly it doesn't work, it gives this error: Fatal error: Call to undefined function mysq_fetch_row() Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099201 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 Oops, sorry. mysq_fetch_row($result) should read mysql_fetch_row($result) Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099203 Share on other sites More sharing options...
NLCJ Posted August 14, 2010 Author Share Posted August 14, 2010 I'm not really familiar with arrays, lets say I'm still a beginning PHP'er and (almost) never made a function. I get this error: Warning: in_array() [function.in-array]: Wrong datatype for second argument in Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099261 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 randNumber($randNumbers); should be $number = randNumber($randomNumbers); Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099266 Share on other sites More sharing options...
NLCJ Posted August 14, 2010 Author Share Posted August 14, 2010 Thanks a lot, works perfectly! Link to comment https://forums.phpfreaks.com/topic/210706-check-for-existence-of-a-random-number/#findComment-1099269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.