目标:向数据库插入50个用户,密码自动生成(随机数),MD5加密
<HTML><BODY>
<?php
//STEP1
//第一步:初始化种子
$seedarray =microtime();
$seedstr =split(" ",$seedarray,5);
$seed =$seedstr[0]*10000;
//第二步:使用种子初始化随机数发生器
srand($seed);
//第三步:生成指定范围内的随机数
//$random1 = rand(100,500);
//STEP2
//连数据库
$dbh = mysql_connect('localhost','admin','newpwd');
mysql_select_db('test');
//STEP3
// 定义
$Matrix = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$MatrixLen = strlen($Matrix);
$Length = 10;
$MaxUsers = 3;
//STEP4
//循环产生随机,并插入数据库
for( $k=0; $k<$MaxUsers; ++$k)
{
$UserPwd = "";
for( $i=0; $i<$Length; ++$i)
{
$UserPwd .= $Matrix{rand(100,500) % $MatrixLen};
}
$UserName = $UserPwd;
$UserPwd = strtoupper(md5($UserPwd));
$query = "INSERT INTO TBL_User (Name,Password) values ('$UserName','$UserPwd')";
echo $query;
echo "<BR>\n\r";
$res = mysql_query($query, $dbh);
$err = mysql_error();
if($err)
{
echo "发生错误!";
}
else
{
echo "成功!";
}
}
?>
</BODY></HTML>