将XML文件中的数据插入MySql DB

时间:2022-09-25 21:24:57

Hello i got problem with something, here it is i have to load xml file which has over 2000 properties, here you can find the structure of the file

您好我遇到了问题,这里是我必须加载xml文件,它有超过2000个属性,在这里你可以找到文件的结构

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

and i'm using the fallowing code

我正在使用休息代码

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

and i got this error

我收到了这个错误

Error

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 '' at line 1

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的''附近使用正确的语法

Please if anyone knows what might be the problem answer me here :)

如果有人知道可能是什么问题,请回答我:)

Thank you

2 个解决方案

#1


0  

This is the format for an INSERT:

这是INSERT的格式:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Where are your opening and closing parentheses for VALUES?

VALUES的开括号和右括号在哪里?

#2


0  

Each of the values in the query are missing single quotes, and you're missing the opening and closing parenthesis. The proper syntax for an INSERT is:

查询中的每个值都缺少单引号,并且您缺少左右括号。 INSERT的正确语法是:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ('value1', 'value2', 'value3')

Yours will look more like:

你的看起来更像是:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES value1, value2, value3

The easiest way to debug is to echo out your $sql varaible so you can see what it looks like so you can quickly spot obvious errors.

最简单的调试方法是回显你的$ sql变量,这样你就可以看到它的样子,这样你就可以快速发现明显的错误。

#1


0  

This is the format for an INSERT:

这是INSERT的格式:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Where are your opening and closing parentheses for VALUES?

VALUES的开括号和右括号在哪里?

#2


0  

Each of the values in the query are missing single quotes, and you're missing the opening and closing parenthesis. The proper syntax for an INSERT is:

查询中的每个值都缺少单引号,并且您缺少左右括号。 INSERT的正确语法是:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ('value1', 'value2', 'value3')

Yours will look more like:

你的看起来更像是:

INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES value1, value2, value3

The easiest way to debug is to echo out your $sql varaible so you can see what it looks like so you can quickly spot obvious errors.

最简单的调试方法是回显你的$ sql变量,这样你就可以看到它的样子,这样你就可以快速发现明显的错误。