PHP while()循环复制MySQL行

时间:2014-09-25 19:05:52

标签: php mysql

我有一个嵌套的while()在另一个内部,它们查询不同的表并从中提取信息。

然后我执行MySQL查询,将所有数据插入到不同的数据库中。

$grad5app = mysqli_query($grad5app_connect, "SELECT * FROM applicant_academic");
while ($row = mysqli_fetch_array($grad5app)) {
    $applicationId = $row[0];
    $startYear = $row[10];
    $startSemester = $row[9];
    $hasBeenSubmitted = $row[2];
    $submittedDate = $row[8];
    $essayOldFileName = $row[15];
    $resumeOldFileName = $row[16];

    if ($startYear == "") {
        $startYear = 'NULL';
    }
    if ($startSemester == "") {
        $startSemester = 'NULL';
    }

    $grad5app1 = mysqli_query($grad5app_connect, "SELECT * FROM applicants");
    while ($row1 = mysqli_fetch_array($grad5app1)) {
        $createdDate = $row1[116];
        $lastModified = $row1[117];
        $desiredHousing = $row1[58];
        $transactionId = $row1[152];
        $hasBeenPushed = $row1[126];
        $pushedDate = $row1[119];
        $hashReference = $row1[8];
        $waiveReferenceViewingRights = $row1[145];
        $hasUmaineCorrespondent = $row1[144];
        $umaineCorrespondentDetails = $row1[81];
        $hasAcceptedTermsOfAgreement = $row1[150];
    }

    mysqli_query($gradschool_application_2_connect, "INSERT INTO Application ".
        "(applicationId, applicantId, applicationTypeId, createdDate, lastModified, startYear, startSemester, desiredHousing, ".
        "hasUmaineCorrespondent, umaineCorrespondentDetails, hasAcceptedTermsOfAgreement, transactionId, hasBeenSubmitted, ".
        "submittedDate, hasBeenPushed, pushedDate, hashReference, waiveReferenceViewingRights, essayOldFileName, resumeOldFileName) ".
        "VALUES ('".$applicationId."', '".$applicationId."', 1, '".$createdDate."', '".$lastModified."', ".$startYear.", ".
        "'".$startSemester."', '".$desiredHousing."', '".$hasUmaineCorrespondent."', '".$umaineCorrespondentDetails."', '".$hasAcceptedTermsOfAgreement."', ".
        "'".$transactionId."', '".$hasBeenSubmitted."', '".$submittedDate."', '".$hasBeenPushed."', '".$pushedDate."', '".$hashReference."', ".
        "'".$waiveReferenceViewingRights."', '".$essayOldFileName."', '".$resumeOldFileName."')");

    echo "INSERTED";
       echo"<br>".$lastModified."<br>";
}

输出:

INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56
INSERTED
2013-11-14 12:10:56

applicants表具有唯一的$lastModified日期,因此不应重复。显然我的while循环结构搞砸了。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

为什么要改变?

While (...) {
   while (...) {
        $lastModified = $row1[117];
   }
   insert into ...
}

由于您在内部while()循环中设置$lastModified,然后在循环完成之后才使用它,它将始终在循环内获取LAST值。查询永远不会更改,因此您始终可以获得相同的记录,这意味着您始终可以获得相同的最终值。

也许你的结构应该更像:

while(...) {
   while(...) {
       $lastModified = ...
       insert into ...
   }
}

代替。