I am new to PHP, and I want to make a code that makes a new random string and checks to see if it exists in a database. This is my code so far. Is there a way to make it better or a more efficient way?:
function generateRandomKey()
{
$randomList=array("A","H","I","P","Q","X","Y","f","g","n","o","v","w","3","4",")",
"B","G","J","O","R","W","Z","e","h","m","p","u","x","2","5","(",
"C","F","K","N","S","V","a","d","i","l","q","t","y","1","6","9",
"D","E","L","M","T","U","b","c","j","k","r","s","z","0","7","8",);
$randomKey="";
for ($i=0; $i<=8; $i=$i+1)
{
$rand=mt_rand(0,63);
$randomKey=$randomKey.$randomList[$rand];
}
$sql="SELECT * FROM blogs WHERE blogRandomKey=".$randomKey.";";
$result=mysqli_query($conn, $sql);
$queryResults=mysqli_num_rows($result);
if ($queryResults>0)
{
generateRandomKey();
}
else
{
return $randomKey;
}
}
Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire