Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a table that is looks like this. This is some of the general information when a user registers on my site.

+------------+------------+
|   user_id  | username   |
|   312543   |   Bobby    |
|   543765   |  Victoria  |
+------------+------------+

I am just wondering, how would you generate a random unique number for user_id? Let’s say a number between 1 and 100 that is not yet in the database. I want to accomplish this using PHP, not SQL.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
710 views
Welcome To Ask or Share your Answers For Others

1 Answer

Improving upon Niet the Dark Absol's answer: You can use uniqid(rand()). uniqid() will generate a number based on your server's internal clock and rand() will prefix it with a random number. So even if two users register in the same tiniest fraction, the rand() prefix will assign them a different prefix with over 99.99999% probability.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...