几个小时以来,我一直在努力在脑海中解决这个问题,但仍然没有成功,所以我走到了这里。
由于某些原因,当代码取消注释查询时,即使数据库中有 3 个日志,循环也只会循环一次,如果我们随后注释查询它会按预期工作。
$sth = $dbh->prepare("SELECT * FROM `savedusers`");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
$run++;
foreach($sites as $site){
$clients = ${CalculateControllerVar($site['site_id'])}->list_clients($site['site_id']);
foreach($clients as $client){
if(strtolower($client->mac) == strtolower($row['Mac'])){
$aps = ${CalculateControllerVar($site['site_id'])}->list_aps($site['site_id'], $client->ap_mac);
$sth = $dbh->prepare("UPDATE `savedusers` SET `Location` = :loc WHERE `Mac` = :mac");
$sth->execute(array(':loc' => $aps[0]->name, ':mac' => $row['Mac']));
}
}
}
}
提前致谢。
最佳答案
这是因为您在注释代码中覆盖了变量 $sth
。将这些行更改为:
$sth2 = $dbh->prepare("UPDATE `savedusers` SET `Location` = :loc WHERE `Mac` = :mac");
$sth2->execute(array(':loc' => $aps[0]->name, ':mac' => $row['Mac']));
它会正常工作。
关于php - 奇怪的 PHP While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41037098/