繁体   English   中英

将数据从XML文件插入MySql DB

[英]Insert data from XML file into MySql DB

您好,我遇到了一些问题,这是我必须加载具有2000多个属性的xml文件,在这里您可以找到文件的结构

http://admin.resales-online.com/live/Resales/Export/CreateXMLFeed.asp?U=RESALES@MOVE2S&P=KKPDRT6986NG&n=1

我正在使用休闲代码

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM