Hướng dẫn mysql random unique number
Hi i need to assign a randomly generated number to some entries into the database and it must be unique. I use:
But ofc there could always be a slightly low chance that 2 random numbers will be the same. Then i could do something like:
But this would only check the number once and there will still be a chance that the new generated number already exists into the db. Any suggestions? Thanks
asked Jan 12, 2012 at 11:41
3 Here is a method which you can use:
?>
muya.dev 9061 gold badge13 silver badges33 bronze badges
answered Jan 12, 2012 at 12:52
Vijeenrosh P.WVijeenrosh P.W 3591 gold badge3 silver badges7 bronze badges Doing a
It is possible to write this code in lesser lines. answered Jan 12, 2012 at 12:12
Salman ASalman A 252k80 gold badges423 silver badges513 bronze badges
--> Now u got unique number in "myNumber" answered Jan 12, 2012 at 11:45
2
strtotime("now") would give you a unique number, but ifyou are using this script to process forms/uploads etc from multiple users, the ip address will just make certain of that (on the off chance that at the exact same second two entries are made... can that even happen?? hmm...) md5() cos i felt like it :D answered Jan 12, 2012 at 11:50
UmutUmut 666 bronze badges 4 |