PHP循环创建重复的数据库行

时间:2013-07-25 12:02:57

标签: php mysql

我正在玩创建一个包含任何密码组合的明文和哈希值的数据库,但是当它们被添加到数据库时,我会在进入下一个组合之前得到许多重复...任何想法至于为什么以及如何阻止它?

<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$numberof=0;
$alphabet = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            for($l=0; $l<count($alphabet); $l++){
                $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
                $hash = md5($final);

                mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");
            }
        }
    }
}
echo $numberof;
?>

2 个答案:

答案 0 :(得分:1)

您有四个嵌套for循环,其中包含计数器$i $j $k$l。但是,您在查询中使用的最终字符串仅使用$i $j$k因此您将有大约26个重复项。我假设您打算将$l的值附加到字符串的末尾?

答案 1 :(得分:0)

删除最后一个循环。我已按以下方式更新您的代码:

for($i=0; $i<count($alphabet); $i++){
    for($j=0; $j<count($alphabet); $j++){
        for($k=0; $k<count($alphabet); $k++){
            $final = $alphabet[$i].$alphabet[$j].$alphabet[$k];
            $hash = md5($final);
            mysqli_query($con,"INSERT INTO hashes (plain, hash) VALUES ('$final', '$hash')");           
        }
    }
}