PHP/MySQL - Best way to create unique random string?

后端 未结 10 2141
囚心锁ツ
囚心锁ツ 2020-12-15 04:20

How do I create a random unique string in MySQL?

when I need to create a random string in PHP I use this function:

public function generateString($le         


        
10条回答
  •  甜味超标
    2020-12-15 05:11

    Assuming 10 characters from the character set a-z, A-Z, 0-9 mean there are (26 + 26 + 10)10 = 8.39299366 × 1017 possible combinations. To calculate the odds of a collision... just 1/x the afore-mentioned number. So I would not be worrying about getting the same string twice. Even if do get the same string again I'll just run the function again in a loop, the only exit condition being that a unique string is found.

提交回复
热议问题