嵌套mysql语句 - 只运行一个循环?

时间:2015-01-06 05:34:07

标签: php mysql

不太确定为什么会发生这种情况,以前从未发生过,但不知怎的,我似乎可以想到这一点。

$listing = $mySql->query("SELECT * FROM `ERRVER` as ev JOIN RELIST as rl on rl.Property_id=ev.Property_id WHERE ev.Corrected =1 AND (ev.Fieldname = 'City' || ev.Fieldname = 'StreetName' || ev.Fieldname = 'StreetNumber')");
    while ($record = $listing->fetchAssoc()) {

         $Location_id=$record['Location_id'];
          if ($record['Fieldname']=='City') {
               $sql="Update RELOC SET City='$record[Real_Value]' WHERE Location_id=$Location_id";
          }
          if ($record['Fieldname']=='StreetNumber') {
               $sql="Update RELOC SET StreetNumber='$record[Real_Value]' WHERE Location_id=$Location_id";
          }
          if ($record['Fieldname']=='StreetName') {
               $sql="Update RELOC SET StreetName='$record[Real_Value]' WHERE Location_id=$Location_id";
          }
          trace($sql);

      if ($update=$mySql->query($sql)) {
        $sql2="Update ERRVER SET Corrected=-1 WHERE Error_id=$record[Error_id]";
      trace($sql2);
      $update2=$mySql->query($sql2);

      }



 }

如果我没有$update而只是做一个echo $sql,那就完美了,但是当我有一个$update它只给我一个循环?我知道,我错过了什么,只是不知道是什么。

1 个答案:

答案 0 :(得分:0)

$listing = $mySql->query("SELECT * FROM `Error_Verify` as ev JOIN Real_Listing as rl on rl.Property_id=ev.Property_id WHERE ev.Corrected =1 AND (ev.Fieldname = 'City')");
    while ($record = $listing->fetchAssoc()) {

             $Location_id[]=$record['Location_id'];
             $Value[]=$record['Real_Value'];
             $errorid[]=$record['Error_id'];
     }
     for ($i=0;$l<count($Location_id);$i++) {
         $sql="Update Real_Location SET City='$Value[$i]' WHERE Location_id=$Location_id[$i]";
         trace($sql);
         if ($mySql->query($sql)) {
                $sql2="Update Error_Verify SET Corrected=-1 WHERE Error_id=$errorid[$i]";
                trace($sql2);
                $mySql->query($sql2);                
         }
     }

虽然以上工作.....我知道它会工作......我仍然不确定为什么原始代码不起作用...嵌套的mysql查询不是一件新事......我还是离开抓住的问题,,,