jkkenzie Posted September 18, 2011 Share Posted September 18, 2011 hi!, is it possible to use rand() to generate unique number that will be saved in a database as a primary key? Since i dont want to have numbers like 00001 incrementing on the table. They dont look like real account numbers... i need something like 45642 or 95452 and the like. thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/ Share on other sites More sharing options...
Pikachu2000 Posted September 18, 2011 Share Posted September 18, 2011 Why not just start the autoincrement numbering at 10251 or whatever? Then you don't have to go jacking around with trying to manipulate a PK index. Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270422 Share on other sites More sharing options...
jkkenzie Posted September 18, 2011 Author Share Posted September 18, 2011 sorry, i said PK to mean Unique.... I needed another unique field with the auto increment from 50000 Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270435 Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 The best solution would probably be to pre-generate a list of random numbers in a random database. When a new user is made, you select the first row from the database, assign that id to the user, and delete the row. Keep in mind, this could cause a race condition, so locking the table may be necessary. This way, you don't have to worry about generating random numbers until you find one that isn't used. That solution wouldn't scale well. I agree with Pikachu though, I think the problem you're trying to solve is better approached in a different way. Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270441 Share on other sites More sharing options...
jkkenzie Posted September 18, 2011 Author Share Posted September 18, 2011 I guess i will have pick the PK for the record saved e.g. 12 and then generate a 3 digit random number then Join with the PK 12 e.g 12763 and this will create a unique id: Will there arise an issue of duplicate PK?? Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270466 Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 If you always add the same amount of random digits, and the PK is unique, then there should not be duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270468 Share on other sites More sharing options...
jkkenzie Posted September 19, 2011 Author Share Posted September 19, 2011 great thanks Quote Link to comment https://forums.phpfreaks.com/topic/247382-generate-random-number-unique-in-a-database/#findComment-1270544 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.