robert_gsfame Posted May 14, 2010 Share Posted May 14, 2010 I have used 4 digits number which is being set randomly together with user's fistname to be inserted into my database, to be their user ID assume i have user and his firstname is JOE so i will have this 4 digits number attached together with his firstname 4561JOE what if i have different user with the same fistname as JOE... how can i prevent this user of having the same 4 digits like what the first JOE had in simple words: i dont want second guy get the same ID with the first one (4561JOE) how can i do it using rand() thx Quote Link to comment Share on other sites More sharing options...
Sergey Popov Posted May 14, 2010 Share Posted May 14, 2010 I would make a loop with say 100 iterations, and on each iteration I would generate random 4 digit code and check if CODE + FIRSTNAME already exists in the db (or in array, if there are no too much users). Once the code with no matches found, exit from loop with break. Quote Link to comment Share on other sites More sharing options...
ignace Posted May 14, 2010 Share Posted May 14, 2010 Just create an ordinary AUTO_INCREMENT field with ZeroFill show their userid as SELECT concat(id, username) AS UID .. CREATE TABLE .. ( id INT ZEROFILL NOT NULL AUTO_INCREMENT .. This way you'll get: 0001IGNACE 0002ROBERT Or create your table and set the start index at 1000 That way you'll get: 1000IGNACE 1001ROBERT 1002SERGEY CREATE TABLE .. ( .. ) AUTO_INCREMENT = 1000 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.