lynxus Posted June 9, 2009 Share Posted June 9, 2009 Hi guys, Im trying to find the best way to put a unique ID in a table. I currently have a userid as the primary key and it auto increments however i would like to be able to put in a 10digit "ie: 1234567890 " number as a "siteid" for each new registration. Any ideas? I dont really want to use php to generate it as i could end up with the same id further down the line. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/ Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 EDIT: Nevermind. What does the "siteid" refer to? Can users have multiple siteids? Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852460 Share on other sites More sharing options...
lynxus Posted June 9, 2009 Author Share Posted June 9, 2009 EDIT: Nevermind. What does the "siteid" refer to? Can users have multiple siteids? Siteid is the value that users use on their html code. siteis is unique for each registration. theres only 1 siteid per user. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852461 Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 I guess I'm confused as to why you need two unique ids for 1 registration. You already have the auto-increment "userid", why do you need another unique value? Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852463 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 There are two ways for a computer to generate a random number with no duplicates - 1) Generate and store all possible values/combinations. Randomly retrieve one and delete it from the pool of available values, 2) Generate a random number of the correct length and check if it has already been used, repeat until you get a value that has not already been used. Using a length for your number that has at least 10 x more combinations than the maximum number of values you will ever need will reduce collisions and allow this method to quickly find unused values as you use up more of the available combinations. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852464 Share on other sites More sharing options...
lynxus Posted June 9, 2009 Author Share Posted June 9, 2009 i know its a little silly, but i want it to use a random 10 digit number rather than 100 101 102 103 104 105... This way, people will find it harder to guess other peoples ID's ( not that its a problem if people do guess. I just want it so its a little harder and they are more spread out acorss many numbers. ) Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852467 Share on other sites More sharing options...
lynxus Posted June 9, 2009 Author Share Posted June 9, 2009 OK looks like it may be a bit too far fetched to do this. What ive done instead to generate something unique is: Result = 923200906091244567354 ( for example ) <?php // Generate unique random number. $num0 = (rand(10,100)); $num1 = date("Ymd"); $num2 = (rand(100,1000)); $num3 = time(); $randnum = $num0 . $num1 . $num2 . $num3; // End generate. ?> Thsi way i have a random number and using the date and time. hoepfully this will create some very odd random uniquie unguessable number. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852479 Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 There could still be some collision. You should check against your database, if it's there, create another one, if not, proceed. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852482 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 And that's a lot more then the 10 digits you specified in the statement of the problem. If you don't care about the length, take a look at this function - http://us.php.net/manual/en/function.uniqid.php Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852485 Share on other sites More sharing options...
lynxus Posted June 9, 2009 Author Share Posted June 9, 2009 There could still be some collision. You should check against your database, if it's there, create another one, if not, proceed. I understand there *could be some collison, However considering it uses date and time, Plus 2 separate random generated smaller numbers mixed in, I think it should be ok. Quote Link to comment https://forums.phpfreaks.com/topic/161539-solved-generate-unique-10-digit-number-on-insert/#findComment-852491 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.