0

因此,我编写了一个 PHP 脚本,以便将我以前的 MySQL 数据库升级到具有新关系和内容的新数据库。一切似乎都运行良好,直到我注意到我的新数据库中缺少一些条目。我的旧数据库显示该表的 322 条记录,而我的新鞋仅显示 95 条记录。因此,由于有一些条目,我不知道问题出在哪里,谁能帮助我?

这是我的代码,我只在具有 bigint、varchar、double 和 float 的表 POIS 中遇到此问题:

<?
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';
?>
4

2 回答 2

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;

等等 :)

这样,如果所有行都已迁移,您可以立即查看(受影响的行数)。

于 2012-09-28T11:29:49.573 回答
0

So i figured it out, the problem was my special characters, so i mysqli real_escape_string() to fix my problem. Thanks anyways guys.

于 2012-10-01T11:34:41.030 回答