在php和mysql中生成3000个不同的14位数字 [英] Generate 3000 different 14-digit numbers in php and mysql

查看:78
本文介绍了在php和mysql中生成3000个不同的14位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建14位PIN码.我想插入3000个不同的引脚号,然后将它们插入mysql.以下代码将仅插入120或127个随机数,而不是3000,然后停止.在给出127个数字之前,它还会循环播放更长的时间.我想要的是3000个随机数.

I want to create PINs numbers with 14 digits. I want to insert 3000 different pin numbers and then insert them into mysql. The below codes will insert only 120 or 127 random numbers instead of 3000 and stop. It also loops for longer time before giving that 127 numbers. And what I want is 3000 random numbers.

这是我的代码:

<form method="post" action=""> <table border=1> <tr>

 <td> <input type="text" name="num" placeholder="Enter total number to be generated e.g 3000" required></td>
                </tr>

            <td> <input type="submit" class="btn btn-primary" name="submit" id="submit" value="Generate Pin">  </td>
            <tr>
                <td></table></form>

    <?php
    if (isset($_POST['submit'])){
       $num = $_POST['num'];    
       for ($index = 0; $index < $num; $index++) {    
           $rand1 = rand(1000000, 9999999); 
           $check = mysql_query("SELECT * FROM pins WHERE pin='$pin' ") or die 
          (mysql_error());
          if(mysql_num_rows($check)>0){
          $index-=1; 
          } 
           $sql=mysql_query("INSERT INTO  pins(randi) VALUES('$rand1')"; } } ?> 

推荐答案

再试一次:

<?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);
             }

         } 

    }
?>

这篇关于在php和mysql中生成3000个不同的14位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆