2012-09-28 123 views
0

因此,我編寫了一個PHP腳本,以便將我以前的MySQL數據庫升級到具有新關係和新功能的新數據庫。一切似乎都工作正常,直到我注意到我的新數據庫中缺少一些條目。我的舊數據庫顯示了該表的322條記錄,而我的新鞋只有95條。因此,由於有些條目是成功的,所以我不知道問題出在哪裏?在MySQL數據庫中缺少數據

這裏是我的代碼,我只有在表POIS這個問題有BIGINT,VARCHAR,雙和float:

<? 
include("mysql.class.php"); 


$db_old = new mysqli("localhost", "root", "admin", "smarturbia_old"); 
$db = new mysqli("localhost", "root", "admin", "smarturbia"); 

$categories = array();; 


$db->query("INSERT INTO categories(name) VALUES('taxi')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('golf')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('usa')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('car')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('moon')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('scooter')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('motocross')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('supercar')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('europe')") or die($db->error.__LINE__); 
$db->query("INSERT INTO categories(name) VALUES('snow')") or die($db->error.__LINE__); 


$result = $db->query("SELECT * FROM categories") or die("Não foi possível selecionar a base de dados"); 


     if($result->num_rows > 0) { 
      while($row = $result->fetch_assoc()) 
       $categories[$row['name']] = $row['id'];   
    } 
    else 
      echo 'NO RESULTS'; 

$result = $db_old->query("SELECT * FROM cities") or die($db_old->error.__LINE__); 


     if($result->num_rows > 0) { 
     while($row_old = $result->fetch_assoc()) { 
         $sql="INSERT INTO `cities` VALUES (".$row_old['id'].",'".$row_old['published']."', '".$row_old['open']."', '".$row_old['path']."', '".$row_old['key']."', '".$row_old['world']."', '".$row_old['name']."', '".$row_old['description']."', '".$row_old['logo']."' , '".$row_old['footer']."', '".$row_old['footer_large']."' , '".$row_old['leftpub']."', '".$row_old['rightpub']."' ,'".$row_old['model']."', ".$row_old['modelxscale'].", ".$row_old['modelyscale'].", ".$row_old['modelzscale'].", ".$row_old['wheelmodelxscale'].", ".$row_old['wheelmodelyscale'].", ".$row_old['wheelmodelzscale'].", '".$row_old['allwheels']."', '".$row_old['frontleftwheel']."', '".$row_old['frontrightwheel']."', '".$row_old['rearleftwheel']."', '".$row_old['rearrightwheel']."', ".$row_old['axisdistance']." , ".$row_old['wheelsdistance']." , ".$row_old['wheelsheight'].", ".$row_old['kms'].", ".$row_old['maxspeed'].", ".$row_old['accel'].", ".$row_old['accelstep'].", ".$row_old['minaccelstep']." , ".$row_old['maxrevspeed'].", ".$row_old['decel']." , ".$row_old['gravity'].", ".$row_old['camheight'].", ".$row_old['camtilt'].", ".$row_old['traildistance'].", ".$row_old['mass'].", ".$row_old['vehicleagility'].", ".$row_old['suspensionstiffness'].", ".$row_old['suspensionrestlength'].", ".$row_old['suspensiondamping'].", ".$row_old['suspensiondeltatime'].", ".$row_old['turnspeedmin'].", ".$row_old['turnspeedmax'].", ".$row_old['speedmaxturn'].", ".$row_old['speedminturn'].", ".$row_old['steerroll'].", ".$row_old['rollspring'].", ".$row_old['rollclamp'].", '".$row_old['mapiconurl']."', '".$row_old['vehicleshadow']."', '".$row_old['vehiclesound']."', ".$row_old['vehiclesoundtime'].", '".$row_old['vehiclefastsound']."', ".$row_old['vehiclefastsoundtime'].", '".$row_old['backgroundsound']."', ".$row_old['backgroundsoundtime'].", '".$row_old['crashsound']."', ".$row_old['crashsoundtime'].", '".$row_old['vehicletype']."', '".$row_old['date']."' , ".$categories[$row_old["path"]]. ")"; 
         try { 
          $db->query($sql); 
         } 
         catch (Exception $e){ 
          echo 'Caught exception: ', $e->getMessage(), "\n"; 
         }      
       } 
    } 
    else { 
     echo 'NO RESULTS'; 
    } 


$result = $db_old->query("SELECT * FROM kmz") or die($db_old->error.__LINE__); 

      if($result->num_rows > 0) { 
     while($row_old = $result->fetch_assoc()) { 
         $sql="INSERT INTO `kmz` VALUES (".$row_old['id'].",".$row_old['city'].", '".$row_old['file']."','".$row_old['date']."')"; 
         try { 
          $db->query($sql); 
         } 
         catch (Exception $e){ 
          echo 'Caught exception: ', $e->getMessage(), "\n"; 
         }      
       } 
    } 
    else { 
     echo 'NO RESULTS'; 
    } 


$result = $db_old->query("SELECT * FROM pois") or die($db_old->error.__LINE__); 

      if($result->num_rows > 0) { 
     while($row_old = $result->fetch_assoc()) { 
         $sql="INSERT INTO `pois` VALUES (".$row_old['id'].",".$row_old['city'].", '".$row_old['name']."','".$row_old['description']."',".$row_old['lat'].", ".$row_old['lon'].",".$row_old['heading'].")"; 
         try { 
          $db->query($sql); 
         } 
         catch (Exception $e){ 
          echo 'Caught exception: ', $e->getMessage(), "\n"; 
         }      
       } 
    } 
    else { 
     echo 'NO RESULTS'; 
    } 


     //Just for consistency 

     try { 
      $db->query("INSERT INTO `smarturbia`.`limits` (`id`, `city`, `area`, `lat`, `lon`, `date`) VALUES (NULL, '76', NULL, '23.34343434', '-3.343343434', CURRENT_TIMESTAMP);"); 
      $db->query("INSERT INTO `smarturbia`.`area` (`id`, `city`, `name`, `date`) VALUES (NULL, '76', 'areateste', CURRENT_TIMESTAMP);"); 
      $db->query("UPDATE `smarturbia`.`limits` SET `area` = '1' WHERE `limits`.`id` =1;"); 

      //for sound 
      $db->query("UPDATE `smarturbia`.`cities` SET `vehiclesound` = '/assets/sounds/vehicle.ogg';"); 
      $db->query("UPDATE `smarturbia`.`cities` SET `backgroundsound` = '/assets/sounds/background.ogg';"); 
      $db->query("UPDATE `smarturbia`.`cities` SET `crashsound` = '/assets/sounds/crash.ogg';"); 


     } 
     catch (Exception $e){ 
      echo 'Caught exception: ', $e->getMessage(), "\n"; 
     }  

     echo 'Done'; 
?> 
+0

您是否可以將缺少的記錄縮小到您正在更新的表中? – MindSpiker

+0

對不起,我不明白你的意思..你的意思是把我的記錄放在另一張桌子上? – psychok7

回答

0

所以我想通了,問題是我的特殊字符,所以我mysqli real_escape_string()來解決我的問題。無論如何,謝謝你們。

0

由於這兩個數據庫駐留在同一臺主機上,你爲什麼不只需使用SQL複製它們?從您的代碼鬆散採取:

INSERT INTO SMARTURBIA.cities 
SELECT SMARTURBIA_OLD.cities.*,catagories.name 
    from SMARTURBIA_OLD.cities, SMARTURBIA.CATEGORIES 
WHERE cities.path=categories.name; 

INSERT INTO SMARTURBIA.kmz SELECT * FROM SMARTURBIA_OLD.kmz; 

INSERT INTO SMARTURBIA.pois SELECT * FROM SMARTURBIA_OLD.pois; 

等等:)

這樣的話,你可以立即看到(受影響的行數)如果所有行都已被遷移。

+0

這個想法是有一個自動化的腳本,這種方式,你建議不是很有效 – psychok7

+0

你可以很容易地把插入到一個.sql腳本並使用'mysql DocJones

+0

但如果我想要更靈活的內容更難,我起初嘗試純SQL,但我覺得用PHP會更靈活,而且我也無法讓它工作,然後 – psychok7