generate unique id in php code example

Example 1: php unique id

uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE ]] ) : string

Example 2: php generate unique id for mysql

A programmatic way can be to:

add a UNIQUE INDEX to the field
generate a random string in PHP
loop in PHP ( while( ! DO_THE_INSERT ) )
generate another string
Note:

This can be dirty, but has the advantage to be DBMS-agnostic
Even 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
the loop is statistically not executed at all, it is entered only on insert failure

Tags:

Php Example