I have a function that generates 5 character long random key.
function generator($lenght=5)
{
for($i = 0; $i <= $lenght; $i++)
{
$uniqueKey .= chr(rand(97,122));
}
return $uniqueKey;
}
$uKey = generator();
Now I need to add step 2 -- query my db to see if this key is already taken. If yes, repeat the first step, generate new key and then check again.
Here's how I query my db:
if ($uKey == $db->get_var("SELECT key FROM t1 WHERE key = '".$uKey."' ") )
{
// key exists
} else {
// key does not exist
}
I can't put it together for a world... Really need some serious help!
echo $usrKey; // abcdefgh