刷新随机“令牌”
问题描述:
我的脚本从文件text.txt中读取电子邮件,我想为每个电子邮件在mySQL数据库中记录电子邮件和唯一随机代码。所有作品都是正确的,但每次我都得到相同的“随机码”。刷新随机“令牌”
I get this:
[email protected] vsdggd
[email protected] vsdggd
[email protected] vsdggd
I want to get:
[email protected] bsgfdg
[email protected] jhngfv
[email protected] sdfasd
代码:
<?php
function generatePassword($length = 6){
$chars = 'abcdefhiknrstyz';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
include('../connect.php');
$query = 'insert into cc (email,token) values ("%s","'. generatePassword(6) .'");';
$lines = file('text.txt');//your filename
for($i=0; $i < count($lines); $i++){
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
}
?>
答
你只调用generatePassword()一次,来修复你的代码,因为它是你想要的:
<?php
function generatePassword($length = 6){
$chars = 'abcdefhiknrstyz';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
include('../connect.php');
$query = 'insert into cc (email,token) values ("%s","%s");';
$lines = file('text.txt');//your filename
for($i=0; $i < count($lines); $i++){
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]), generatePassword(6))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
}
?>
这样generatePassword()被调用为每一行。
此外,你应该真的使用较新的mysqli或理想的PDO作为旧的mysql已弃用,并已从PHP7中删除。
请更正您的问题!解释你的意思是什么,最重要的是你想要做什么。 –