Good afternoon, I have the following code to generate a sequence of letters and numbers.
function aleatorio($longitud, $tipo=0){
$codigo = "";
$caracteres="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$max=strlen($caracteres)-1;
for($i=0;$i < $longitud;$i++)
{
$codigo.=$caracteres[rand(0,$max)];
}
return $codigo;
}
and I call the function so that it gives me a random one.
My new problem is that I need to create a random and check that it doesn't exist in the database and I don't know how to do it, the idea is that it is a unique random in the database.
With this it works you just have to adapt it to your database table
This function creates random numbers for you, you can call it when you are saving in the database and to make the comparison with the existing records
Here you make the call to the function which you will be able to manipulate in your PHP to make the respective comparisons
Your query in the database to check if that code already exists
And finally....
You return the obtained result.