Jump to content

looping 4 digits number


robert_gsfame

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.