[英]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.