所以我编写了一个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';
?>
答案 0 :(得分: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;
依此类推:))
这样,如果已迁移所有行,您可以立即看到(受影响的行数)。
答案 1 :(得分:0)
所以我想出来了,问题是我的特殊角色,所以我mysqli real_escape_string()来解决我的问题。不管怎样,谢谢你们。