If all you need is a unique field, and don’t care if it’s numeric or not, then use an AUTO-INCREMENT MySQL field.
If you don’t want it to be numeric, but don’t care for security much (people can guess it), use MD5(AUTO-INCREMENT MySQL field).
If you care a bit about people not guessing it, use MD5(‘AUTO-INCREMENT MySQL field’. ‘my special key’ . time())
If you want some text that people can “read”, you can do this:
Make your foo field unique.
generate your text, something like this:
# try 10 times
$i = 0;
while (!mysql_query("INSERT INTO table SET foo = '".getPass()."'") && $i<10) {
$i++;
}
# generate a readable text
function getPass($length = 6) {
$conso=array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
$vocal=array("a","e","i","o","u");
$password="";
srand ((double)microtime()*1000000);
$max = ceil($length/2);
for($i=1; $i<=$max; $i++) {
$password.=$conso[rand(0,19)];
$password.=$vocal[rand(0,4)];
}
return $password;
}
If you care about performance, you will make foo an index, generate a few strings, then
SELECT foo FROM table WHERE foo IN ('.join(',',$optionsArray).');
See what was found, and insert something it was not found. (do this in a loop)
There are many better ways to do this, but creating them 1 by 1 and doing selects in loops is not a good idea.