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
https://forums.phpfreaks.com/topic/201718-looping-4-digits-number/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.