I want to generate a random number code in PHP without repeat and with 16 lengths. What's the best way to do this? I use this code :
$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 14) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
echo($code);
But that is generating 1 random number. I want 30000 random numbers. What shall I do ?
i use this code too but that is not generate 16 length :
<?php
$con=mysqli_connect("localhost","root","","test1");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
for ($s=0;$s<10;$s++) {
$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 16) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
echo($code);
echo nl2br("$code <br/>");
mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
VALUES ('', '".$code."','1', '0')");
}
for ($s=0;$s<10;$s++) {
$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 16) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
echo($code);
echo nl2br("$code <br/>");
mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
VALUES ('', '".$code."','2', '0')");
}
for ($s=0;$s<10;$s++) {
$possible = '0123456789';
$code = '';
$i = 0;
while ($i < 16) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
echo($code);
echo nl2br("$code <br/>");
mysqli_query($con,"INSERT INTO test (ID, Code, Type, Used)
VALUES ('', '".$code."','3', '0')");
}
mysqli_close($con);
?>