gpt4 book ai didi

PHP while() 循环复制 MySQL 行

转载 作者:行者123 更新时间:2023-11-30 22:56:44 28 4
gpt4 key购买 nike

我有一个嵌套的 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 循环结构被搞砸了。谁能帮帮我?

最佳答案

为什么要改变?

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

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

也许你的结构应该更像:

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

相反。

关于PHP while() 循环复制 MySQL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26046190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com