PHP - 生成特殊的唯一ID

Okay, so just rewriting the Question - so that you see what i did until now and what my Question really means!

For a Webapplication i am trying to build i need IDs which are 5 characters long. I want to use A-Z and 0-9 for the IDs to have more possible IDs. If there are now 5 character long IDs left - the System should notice it and change to 6 charaters - and so on.

I already know how to build the IDs with the characters I want to use - but my Question is: Is there a better Way to check if the ID is already in use or to ensure that the id can't be in use as doing a recursion between generateID -> try to insert -> on fail -> restart -> ... ?

For something like this, the only way to guarantee uniqueness without checking is to use some function that has a one-to-one mapping to something that is already guaranteed to be unique, such as an existing record ID which your database schema probably already contains. The simplest case I can think of would be to take the hexadecimal representation of such a record ID using dechex($id).

A mapping function that generates a YouTube-like alphanumeric ID from a numeric ID and vice versa is discussed here: Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL