2012-01-30 134 views
1

我剛剛完成了一個腳本來從XML文件導入數據到MySQL,我有超過280,000+行,但導入完成後,我只看到91,000行。驗證如果每個查詢是沒有任何問題運行後,我發現下面的一個:MySQL插入查詢沒有運行

INSERT INTO `hotels` (`hotelId`, `hotelFileName`, `hotelName`, `rating`, `cityId`, `cityFileName`, `cityName`, `stateId`, `stateFileName`, `stateName`, `countryCode`, `countryFileName`, `countryName`, `imageId`, `Address`, `minRate`, `currencyCode`, `Latitude`, `Longitude`, `NumberOfReviews`, `ConsumerRating`, `PropertyType`, `ChainID`, `Facilities`) VALUES (1568054, 'Der_Obere_Wirt_zum_Queri', '\"Der Obere Wirt\" zum Queri', 3, 34633, 'Andechs', 'Andechs', 212, 'Bavaria', 'Bavaria', 'DE', 'Germany', 'Germany', 51498149, 'Georg Queri Ring 9', 92.1476, 'EUR', '48.009423000000', '11.214504000000', 16, 8.5, 0, 0, '1|3|5|8|22|27|45|49|53|56|64|66|67|139|202|209|213|256|') 

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /home/hotels/public_html/insert.php on line 57 
INSERT INTO `hotels` (`hotelId`, `hotelFileName`, `hotelName`, `rating`, `cityId`, `cityFileName`, `cityName`, `stateId`, `stateFileName`, `stateName`, `countryCode`, `countryFileName`, `countryName`, `imageId`, `Address`, `minRate`, `currencyCode`, `Latitude`, `Longitude`, `NumberOfReviews`, `ConsumerRating`, `PropertyType`, `ChainID`, `Facilities`) VALUES (1658359, 'Seclusions_of_Yallingup', '\"Seclusions\" of Yallingup', 4, 72257, 'Yallingup', 'Yallingup', 172, 'Western_Australia', 'Western Australia', 'AU', 'Australia', 'Australia', 53234107, '58 Zamia Grove', 238.7800, 'AUD', '-33.691192000000', '115.061938999999', 0, 0, 3, 0, '3|6|13|14|21|22|28|39|40|41|51|53|54|56|57|58|65|66|141|191|202|204|209|210|211|292|') 

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /home/hotels/public_html/insert.php on line 57 
INSERT INTO `hotels` (`hotelId`, `hotelFileName`, `hotelName`, `rating`, `cityId`, `cityFileName`, `cityName`, `stateId`, `stateFileName`, `stateName`, `countryCode`, `countryFileName`, `countryName`, `imageId`, `Address`, `minRate`, `currencyCode`, `Latitude`, `Longitude`, `NumberOfReviews`, `ConsumerRating`, `PropertyType`, `ChainID`, `Facilities`) VALUES (1491947, '1_Melrose_Blvd', '#1 Melrose Blvd', 5, 964, 'Johannesburg', 'Johannesburg', NULL, '', '', 'ZA', 'South_Africa', 'South Africa', 46777171, '1 Melrose Boulevard Melrose Arch', , 'ZAR', '-26.135656000000', '28.067751000000', 0, 0, 9, 0, '6|7|9|11|12|15|17|18|21|32|34|39|41|42|50|51|56|58|60|140|173|202|293|296|') 
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'ZAR', '-26.135656000000', '28.067751000000', 0, 0, 9, 0, '6|7|9|11|12|15|17|18' at line 1 

這是我的查詢代碼:

if($obj->stateFileName != "") 
    { 
    $query = "INSERT INTO `hotels` (`hotelId`, `hotelFileName`, `hotelName`, `rating`, `cityId`, `cityFileName`, `cityName`, `stateId`, `stateFileName`, `stateName`, `countryCode`, `countryFileName`, `countryName`, `imageId`, `Address`, `minRate`, `currencyCode`, `Latitude`, `Longitude`, `NumberOfReviews`, `ConsumerRating`, `PropertyType`, `ChainID`, `Facilities`) VALUES ({$obj->hotelId}, '".mysql_real_escape_string($obj->hotelFileName)."', '".mysql_real_escape_string($obj->hotelName)."', {$obj->rating}, {$obj->cityId}, '".mysql_real_escape_string($obj->cityFileName)."', '".mysql_real_escape_string($obj->cityName)."', {$obj->stateId}, '".mysql_real_escape_string($obj->stateFileName)."', '".mysql_real_escape_string($obj->stateName)."', '{$obj->countryCode}', '".mysql_real_escape_string($obj->countryFileName)."', '".mysql_real_escape_string($obj->countryName)."', {$obj->imageId}, '".mysql_real_escape_string($obj->Address)."', {$obj->minRate}, '{$obj->currencyCode}', '{$obj->Latitude}', '{$obj->Longitude}', {$obj->NumberOfReviews}, {$obj->ConsumerRating}, {$obj->PropertyType}, {$obj->ChainID}, '{$obj->Facilities}')"; 
    $result = mysql_query($query); 
    } 
    else 
    { 
    $query = "INSERT INTO `hotels` (`hotelId`, `hotelFileName`, `hotelName`, `rating`, `cityId`, `cityFileName`, `cityName`, `stateId`, `stateFileName`, `stateName`, `countryCode`, `countryFileName`, `countryName`, `imageId`, `Address`, `minRate`, `currencyCode`, `Latitude`, `Longitude`, `NumberOfReviews`, `ConsumerRating`, `PropertyType`, `ChainID`, `Facilities`) VALUES ({$obj->hotelId}, '".mysql_real_escape_string($obj->hotelFileName)."', '".mysql_real_escape_string($obj->hotelName)."', {$obj->rating}, {$obj->cityId}, '".mysql_real_escape_string($obj->cityFileName)."', '".mysql_real_escape_string($obj->cityName)."', NULL, '', '', '{$obj->countryCode}', '".mysql_real_escape_string($obj->countryFileName)."', '".mysql_real_escape_string($obj->countryName)."', {$obj->imageId}, '".mysql_real_escape_string($obj->Address)."', {$obj->minRate}, '{$obj->currencyCode}', '{$obj->Latitude}', '{$obj->Longitude}', {$obj->NumberOfReviews}, {$obj->ConsumerRating}, {$obj->PropertyType}, {$obj->ChainID}, '{$obj->Facilities}')"; 
    $result = mysql_query($query); 
    } 

回答

4

對於You have an error in your SQL syntax錯誤,您可以查看實際查詢,它告訴您發現問題。搜索ZAR我發現這一點:

..., '1 Melrose Boulevard Melrose Arch', , 'ZAR', '-26.135656000000', ... 

正如你所看到的,, ,是無效的語法,它缺少的元素。看看你的代碼,它似乎缺少{$obj->minRate}。該字段對於某些記錄是空的(或者至少對於您發佈的一條記錄)。爲了解決這個問題,你可以嘗試更換與:

is_null($obj->minRate) ? 'NULL' : $obj->minRate 

對於mysql_free_result警告,你有沒有示出,其中該方法被調用的代碼,所以很難說什麼是什麼。

3

插入語法是無效的,它包含兩個逗號,沒有價值:'1 Melrose Boulevard Melrose Arch', , 'ZAR',

嘗試將其更改爲: '1梅爾羅斯大道的Melrose Arch',NULL, 'ZAR',

此外,這可能意味着無論將XML轉換爲SQL插入語句都有錯誤。但是您沒有提供任何代碼,因此您無法在此處幫助您解決問題。