[英]Insert data from XML file into MySql DB
您好,我遇到了一些問題,這是我必須加載具有2000多個屬性的xml文件,在這里您可以找到文件的結構
我正在使用休閑代碼
<html>
<head>
<title>Insert Record</title>
</head>
<body>
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(-1);
include ('config.php');
$url = "http://admin.resales-online.com/live/Resales/Export/XMLFeed.asp?U=RESALES@MOVE2S&P=KKPDRT6986NG&n=100";
try{
$xml = new SimpleXMLElement($url, null, true);
}catch(Exception $e){
echo $e->getMessage();
exit;
}
$sql = 'INSERT INTO properties (`id`,`status_date`,`status`,`listed_date`,`last_updated`,`price`,`currency`,`country`,`province`) VALUES ';
foreach($xml->property as $property){
$sql .= sprintf("\n",
mysql_real_escape_string($property->id),
mysql_real_escape_string($property->status_date),
mysql_real_escape_string($property->listed_date),
mysql_real_escape_string($property->last_updated),
mysql_real_escape_string($property->price),
mysql_real_escape_string($property->currency),
mysql_real_escape_string($property->country),
mysql_real_escape_string($property->province)
);
}
$sql = rtrim($sql, ',') . ';';
if(!mysql_query($sql)){
echo '<h1 style="color: red;">Error</h1><p>', mysql_error(), '</p>';
}
?>
</body>
</html>
我得到這個錯誤
錯誤
您的SQL語法有誤; 檢查與您的MySQL服務器版本相對應的手冊,以在第1行的''附近使用正確的語法
如果有人知道可能是什么問題,請在這里回答我:)
謝謝
這是INSERT的格式:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
VALUES的左括號和右括號在哪里?
查詢中的每個值都缺少單引號,並且您缺少左括號和右括號。 INSERT的正確語法是:
INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ('value1', 'value2', 'value3')
您的看起來會更像:
INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES value1, value2, value3
調試的最簡單方法是回顯$sql
變量,以便查看其外觀,以便快速發現明顯的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.