@MohamedFadili You need to first generate your string using generateRandomString();
then check if it exists in database in that case you need to keep trying till you find one that does not exist, to avoid a dead loop, you can throw an exception after X attempts (X = 10 maybe ?) so the code would look something like below :
a. If you have a unique constraint in your database (it would throw an exception when you insert)
$attempts = 10;
$i = 0;
$inserted = false;
while (!$inserted || $i < $attempts) {
try {
$myRandomString = generateRandomString();
// MySQL CODE TO DO YOUR INSERT HERE IT COULD THROW AN EXCEPTION IF THE STRING ALREADY EXIST IN DATABASE
// here if insertion operation was successful you set $inserted variable to true
$inserted = true;
} catch (PDOException $ex){
// do nothing
}
}
b. if no unique index constraint in database, you need to check by your self:
$attempts = 10;
$inserted = false;
while (!$inserted || $i < $attempts){
$myRandomString = generateRandomString();
// SQL CODE TO CHECK IF $myRandomString Exist
// IF NOT EXIST DO INSERT AND SET $inserted to true
$inserted = true;
// ELSE IF EXIST KEEP TRYING
}