且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在php和mysql中生成3000个不同的14位数字

更新时间:2023-02-10 15:55:50

再试一次:

<?php 
    if (isset($_POST['submit'])){
    // amount of numbers
    $num = $_POST['num'];
    // array of different numbers    
    $num_ar = [];
         // infinity loop
         for ( ; ; ) {    
             // random number
             $rand1 = rand(1000000, 9999999); 
             // 14-digits
             //$rand1 = rand(10000000000000, 99999999999999);  
             // if it is a different number
             if(!in_array($rand1,$num_ar)) {

                 $sql = mysql_query("INSERT INTO  pins(randi) VALUES('$rand1')";
                 array_push($num_ar,$rand1);
             }
             // stop the loop
             if (count($num_ar) == $num) break; 

         } 

    }
?>

<?php 
    if (isset($_POST['submit'])){
    // amount of numbers
    $num = $_POST['num'];
    // array of different numbers    
    $num_ar = [];
         // while loop
         while (count($num_ar) !== $num) {    
             // random number
             $rand1 = rand(1000000, 9999999); 
             // 14-digits
             //$rand1 = rand(10000000000000, 99999999999999);  
             // if it is a different number
             if(!in_array($rand1,$num_ar)) {

                 $sql = mysql_query("INSERT INTO  pins(randi) VALUES('$rand1')";
                 array_push($num_ar,$rand1);
             }

         } 

    }
?>