2012-07-03 17 views

回答

0
$num = mt_rand(10000000, 99999999); 
3

要得到數量,使用mt_rand()

$number = mt_rand(10000000, 99999999); 

若要將其存儲在數據庫中,與PDO插入:

$pdo = new PDO('', $db_username, $db_password); // read the docs for this 
$stmt = $pdo->prepare('INSERT INTO mytable (column_name) VALUES (?)'); 
$stmt->execute(array($number)); 
0

試試這個

$random=mt_rand(10000000, 99999999); 

然後存儲是爲使用SQL 所以您的查詢應該是這樣的

$query="INSERT INTO table_name ('column1') value('$random')";//This depends on your database, edit accordingly. 
mysql_result($query); 
2

考慮其他的解決方案被人爲限制的數字的範圍內,以滿足業務方案的要求,你的數據庫,這裏有另外一個可能的工作,雖然不是很在同樣的方式:

$random = sprintf('%08d', mt_rand(0,99999999)); 
+0

如果存儲在一個整數列中,字符串格式化會有什麼區別嗎?最好給列指定一個明確的寬度並使用'ZEROFILL'?或者只是做數據檢索的格式。 – eggyal

+1

真的夠了,但這一切都取決於OP如何/在哪裏使用數字。 –

+0

「* ...並將該值存儲在mysql表*中」,否? – eggyal

相關問題