jkkenzie Posted July 29, 2009 Share Posted July 29, 2009 Hi!, I have a database of about 10,000 records. I would like to generate a membership number for each record. I would like to mix text and numbers in my generated membership number. What is the best way and functions to use. thanks in advance. Joe Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/ Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 a quick and dirty way would be to create a md5 hash of the results of rand() in mysql... SELECT MD5(RAND()); and then you would have to check to make sure it doesn't already exist, even though it would be highly unlikely... maybe a better solution would to base the id off of existing data of each member, to make it a bit more meaningful and not completely random... and maybe mix it with a bit of randomness with rand()... just rambling, hope this helps... Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/#findComment-885690 Share on other sites More sharing options...
RichardRotterdam Posted July 29, 2009 Share Posted July 29, 2009 What about using hexdecimal numbers and increasing each record hexnumber by one? Then you don't have to check if the number doesn't already exists. The reason for using letters as wel as numbers as id's is usually to shorten the length of the number of characters and not to just make it look cool. Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/#findComment-885786 Share on other sites More sharing options...
patrickmvi Posted July 29, 2009 Share Posted July 29, 2009 Here's a function that will generate a random string of whatever size you'd want. You of course have to check the DB to see if your random string already exists for another member before setting it. function rand_gen($size) { $chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"; $rand_str = ""; for($i = 0; $i < $size; $i++) { $rand_str .= substr($chars, rand(0, 61), 1); } return $rand_str; } Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/#findComment-885807 Share on other sites More sharing options...
Adam Posted July 29, 2009 Share Posted July 29, 2009 Or just add an auto_incrementing ID field to the table and it will automatically assign a unique ID for every record? Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/#findComment-885817 Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 having to go back and check to see if the currently generated one has not already been generated is very inefficient, especially with 10k records. md5 + microtime + loop = win. Quote Link to comment https://forums.phpfreaks.com/topic/167922-solved-generate-unique-number-plus-texts-for-each-record/#findComment-885885 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.