How to generate unique id in MySQL?

前端 未结 16 2648
生来不讨喜
生来不讨喜 2020-11-29 03:17

I\'m programming a script using PHP and MySQL and I want to get a unique id (consisting of a string: capitals and small letters with numbers) like: gHYtUUi5b.

16条回答
  •  不知归路
    2020-11-29 04:10

    crypt() as suggested and store salt in some configuration file, Start salt from 1 and if you find duplicate move to next value 2. You can use 2 chars, but that will give you enough combination for salt.

    You can generate string from openssl_random_pseudo_bytes(8). So this should give random and short string (11 char) when run with crypt().

    Remove salt from result and there will be only 11 chars that should be enough random for 100+ millions if you change salt on every fail of random.

提交回复
热议问题