php 生成一个随机的10位数字并插入到mysql中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23362930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:39:57  来源:igfitidea点击:

Generate a random 10 digit number and insert into mysql?

phpmysql

提问by user3584968

I am trying to generate a random 10 digit number and insert this into mysql on page load, so each time the page refreshes or loads there should be a different 10 digit number being inserted into the database, however at the moment it just keeps inserting the same 10 digit number again and again.

我正在尝试生成一个随机的 10 位数字并在页面加载时将其插入到 mysql 中,因此每次页面刷新或加载时,都应该将不同的 10 位数字插入到数据库中,但是目前它只是不断插入一次又一次地重复相同的 10 位数字。

Can someone please show me where I am going wrong? Thanks.

有人可以告诉我我哪里出错了吗?谢谢。

<?php
    session_start();
    $db_hostname = 'localhost';
    $db_database = 'hewden1'; 
    $db_username = 'root';
    $db_password = '';
    $db_server = mysql_connect($db_hostname, $db_username, $db_password)    
            or die("Unable to connect to MySQL: " . mysql_error());
    mysql_select_db($db_database)   
    or die("Unable to select database: " . mysql_error());

    $num0 = (rand(10,100));
    $num1 = date("Ymd");
    $num2 = (rand(100,1000));
    $num3 = time();
    $randnum = $num0 . $num1 . $num2 . $num3;


    $sql="INSERT INTO supplier_session (session_number)
    VALUES ('$randnum')";

    $result = mysql_query($sql); 
    ?>

This is the number I am constantly getting: 2147483647

这是我不断得到的号码:2147483647

回答by Rakesh Sharma

try to getting ten digit rand no

尝试获得十位数兰特没有

$randnum = rand(1111111111,9999999999);

回答by Vasil Nikolov

You need to change column type. The number that you try to insert is higher than column maximum value (in your case SIGNED INT - 2147483647)

您需要更改列类型。您尝试插入的数字高于列最大值(在您的情况下为 SIGNED INT - 2147483647)

SIGNED INT - 2147483647
UNSIGNED INT - 4294967295

SIGNED BIGINT - 9223372036854775807
UNSIGNED BIGINT - 18446744073709551615

回答by Professor

You can also try this if you want. Its not generating a random number but a random string

如果你愿意,你也可以试试这个。它不是生成随机数而是随机字符串

private string generaterandomnumber()
        {
            string Rand1 = RandomString(8);
            string Rand2 = RandomString(8);
            docNum = Rand1 + "-" + Rand2;
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(docNum));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }

Downvoting not required!!! Thx

不需要投票!!!谢谢

回答by h2ooooooo

You're getting 2147483647because it's the highest signed 32 bit integer that exists (binary it's 1111111111111111111111111111111). This is also the contents of the constant PHP_INT_MAX.

你得到的2147483647是因为它是存在的最高有符号 32 位整数(二进制它是1111111111111111111111111111111)。这也是常量的内容PHP_INT_MAX

Just use a string with a loop and the mt_rand()function:

只需使用带有循环和mt_rand()函数的字符串:

<?php

for ($randomNumber = mt_rand(1, 9), $i = 1; $i < 10; $i++) {
    $randomNumber .= mt_rand(0, 9);
}

var_dump($randomNumber); //eg. string(10) "9152676628"

DEMO

演示

回答by Kami

You should consider using the mt_randfunction as gives a better distribution range.

您应该考虑使用该mt_rand函数,因为它提供了更好的分布范围。

In your code, you are not using the full 10 digit number range, try something like

在您的代码中,您没有使用完整的 10 位数字范围,请尝试类似

$MIN_SESSION_ID = 1000000000;
$MAX_SESSION_ID = 9999999999;

$randId = mt_rand($MIN_SESSION_ID, $MAX_SESSION_ID);

echo $randId;

回答by Danieloplata

There is no problem with your number generating code. Try unsetting your variables before they are defined and see if it fixes the problem.

您的号码生成代码没有问题。在定义变量之前尝试取消设置它们,看看它是否能解决问题。

unset($num0, $num1, $num2, $num3);
$num0 = (rand(10,100));
$num1 = date("Ymd");
$num2 = (rand(100,1000));
$num3 = time();
$randnum = $num0 . $num1 . $num2 . $num3;

You could just as easily use the randfunction to generate a 10 digit number by using the following code: rand(1111111111,9999999999)

您可以rand使用以下代码轻松地使用该函数生成一个 10 位数字:rand(1111111111,9999999999)

回答by Manibharathi

Use this code.

使用此代码。

$x = 10; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);

回答by dkakoti

Use php mt_rand() function instead of rand()

使用 php mt_rand() 函数代替 rand()

<?php
echo mt_rand();
?>

回答by alreadycoded.com

function random_num ($len)
{
 $ch = "0123456789";
 $l = strlen ($ch) - 1;
 $str = "";
 for ($i=0; $i < $len; $i++)
 {
     $x = rand (0, $l);
     $str .= $ch[$x];
 }
 return $str;
}

回答by Wil

When I run your script I get different numbers everytime. Why don't you use the uniqid() function ?

当我运行你的脚本时,我每次都会得到不同的数字。为什么不使用 uniqid() 函数?