php generate unique id for mysql

Solutions on MaxInterview for php generate unique id for mysql by the best coders in the world

showing results for - "php generate unique id for mysql"
Eleonora
24 Oct 2020
1A programmatic way can be to:
2
3add a UNIQUE INDEX to the field
4generate a random string in PHP
5loop in PHP ( while( ! DO_THE_INSERT ) )
6generate another string
7Note:
8
9This can be dirty, but has the advantage to be DBMS-agnostic
10Even if you choose to use a DBMS specific unique ID generator function (UUID, etc) it is a best practice to assure the field HAS to be UNIQUE, using the index
11the loop is statistically not executed at all, it is entered only on insert failure