![](/img/trans.png)
[英]How to use php to generate random 10 digit number that begins with the same two digits
[英]Generate a random 10 digit number and insert into mysql?
我正在尝试生成一个随机的 10 位数字并在页面加载时将其插入到 mysql 中,因此每次页面刷新或加载时,都应该将不同的 10 位数字插入到数据库中,但是目前它只是不断插入一次又一次地重复相同的 10 位数字。
有人可以告诉我我哪里出错了吗? 谢谢。
<?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);
?>
这是我不断得到的号码:2147483647
试着得到十位数的兰特没有
$randnum = rand(1111111111,9999999999);
您需要更改列类型。 您尝试插入的数字高于列最大值(在您的情况下为SIGNED INT - 2147483647)
SIGNED INT - 2147483647
UNSIGNED INT - 4294967295
SIGNED BIGINT - 9223372036854775807
UNSIGNED BIGINT - 18446744073709551615
你得到2147483647
因为它是存在的最高签名32位整数(二进制它是1111111111111111111111111111111
)。 这也是常量PHP_INT_MAX
的内容。
只需使用带循环的字符串和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"
如果你愿意,你也可以尝试这个。 它不生成随机数而是随机字符串
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不需要!!! 谢谢
您应该考虑使用mt_rand
函数作为更好的分布范围。
在你的代码中,你没有使用完整的10位数字范围,尝试类似的东西
$MIN_SESSION_ID = 1000000000;
$MAX_SESSION_ID = 9999999999;
$randId = mt_rand($MIN_SESSION_ID, $MAX_SESSION_ID);
echo $randId;
使用此代码。
$x = 10; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);
使用php mt_rand()函数代替rand()
<?php
echo mt_rand();
?>
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;
}
当我运行你的脚本时,我每次都得到不同的数字。 你为什么不使用uniqid()函数?
您的号码生成代码没有问题。 尝试在定义变量之前取消设置,看看它是否能解决问题。
unset($num0, $num1, $num2, $num3);
$num0 = (rand(10,100));
$num1 = date("Ymd");
$num2 = (rand(100,1000));
$num3 = time();
$randnum = $num0 . $num1 . $num2 . $num3;
您可以使用以下代码轻松使用rand
函数生成10位数字: rand(1111111111,9999999999)
这是生成10位随机数的简单方法
<?php
function generateRandomNumber($length = 10) {
$number = '1234567890';
$numberLength = strlen($number);
$randomNumber = '';
for ($i = 0; $i < $length; $i++) {
$randomNumber .= $number[rand(0, $numberLength - 1)];
}
return $randomNumber;
}
echo generateRandomNumber();
?>
记住当您确实生成此密钥/号码时最合适的方法
echo $key = time();
它的 10 位数字将其转换为确切的日期和时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.