我一直试图在脑子里工作几个小时但仍然没有运气,所以我来到这里。
由于某些原因,当代码取消注释查询时,即使数据库中有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']));
}
}
}
}
提前致谢。
答案 0 :(得分:2)
这是因为您在评论代码中覆盖了变量$sth
。将这些行更改为:
$sth2 = $dbh->prepare("UPDATE `savedusers` SET `Location` = :loc WHERE `Mac` = :mac");
$sth2->execute(array(':loc' => $aps[0]->name, ':mac' => $row['Mac']));
它会正常工作。