我必须使用随机的唯一密码更新所有现有用户的密码。我大约有4000个使用SQL SERVER的用户。
我的代码是
$userSQL = "SELECT a002UserID FROM tbl002password WHERE a002UserID LIKE 'non%'";
$userrs = $db->query($userSQL);
$userList1 = array();
while($row = $userrs->fetch(PDO::FETCH_OBJ)){
$userList1[] = $row->a002UserID;
}
foreach($userList1 as $uid){
function generatePassword ($length = 8)
{
$password = '';
$chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
for ($i = 0; $i < $length; $i ++) {
$password .= $chars[array_rand($chars)];
}
return $password;
}
$pass_gen = generate_password();
$insertPass = "UPDATE tbl002password SET a002password = '$pass_gen' WHERE a002UserID = '$uid'";
$db->exec($insertPass);
echo "updated $uid";
}
如果删除“ for”循环
答案 0 :(得分:1)
// 1. Don't declare functions inside loops. Functions should generally only ever be declared
// in either the global scope, or a class definition.
function generatePassword ($length = 8) {
$password = '';
$chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
for ($i = 0; $i < $length; $i ++) {
$password .= $chars[array_rand($chars)];
}
return $password;
}
$userSQL = "SELECT a002UserID FROM tbl002password WHERE a002UserID LIKE 'non%'";
$userrs = $db->query($userSQL);
$userList1 = array();
while($row = $userrs->fetch(PDO::FETCH_OBJ)) {
$userList1[] = $row->a002UserID;
}
// 2. Preparing statements like this increase performance as they are only parsed _once_,
// but executed many times.
$insertPass = $db->prepare("UPDATE tbl002password SET a002password = ? WHERE a002UserID = ?");
foreach($userList1 as $uid) {
$pass_gen = generate_password();
$insertPass->execute([$pass_gen, $uid]);
echo "updated $uid";
}
答案 1 :(得分:1)
为什么要遍历4,000行?
我将应用一次更新
示例
@SRR1111111.1 1/1
CTGGANAAGTGAAATAATATAAATTTTTCCACTATTGAATAAAAGCAACTTAAATTTTCTAAGTCG
+
AAAAA#EEEEEEEEEEEEEEEEEEEEEEEAEEEEEEEEEEEEEEEEEEEEEEEEEA<AAEEEEE<6
@SRR1111111.3 3/1
CTATANTATTGAAATAATAATGTAGATAAAACTATTGAATAACAGCAACTTAAATTTTCAATAAGA
+
AAAAA#EE6EEEEEEEEEEEEAAEEAEEEEEEEEEEEE/EAE/EAE/EA/EAEAAAE//EEAEAA6
更新后的表格
Declare @tbl002password table (a002UserID varchar(50),a002password varchar(50))
Insert Into @tbl002password values
('JS1234' ,'Password1')
,('nonTI5656','Password2')
,('nonSC5657','Password3')
Update @tbl002password set a002password=right(NewID(),3)+left(abs(convert(bigint,HashBytes('MD5', a002UserID))),5)
Where a002UserID like 'non%'
Select *
From @tbl002password
答案 2 :(得分:0)
我刚刚在沙盒中运行了此
$userList1 = array(1, 2, 3, 4, 5);
function generatePassword ($length = 8)
{
$password = '';
$chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
for ($i = 0; $i < $length; $i ++) {
$password .= $chars[array_rand($chars)];
}
return $password;
};
foreach($userList1 as $uid){
$pass_gen = generatePassword();
echo $pass_gen . ' ';
}
结果是这样的:
ve6XzQzA cL93fvVH GtdP3LP3 8rKDRGJ2 qjt9j6Rv
对我来说看起来不错。您的代码中存在一个明显的语法错误。您称“ generate_password();”而该函数实际上称为“ generatePassword”。我同意Raymond Nijland关于您的SQL的观点。我建议您也更改此设置。