doulao7998636570 2015-10-28 09:50
浏览 23
已采纳

将XML数据发布到MySQL是否会返回错误?

Hi there I am trying to pull XML data from this website (rates.fxcm.com/RatesXML3) and then I was going to put it into an MySQL database. First, the cURL functions are being used to pull this data,

$url ="http://rates.fxcm.com/RatesXML3";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);


$data = curl_exec($ch);
curl_close($ch);

then SimpleXML parses the data into what I believe is an array of some form:

$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);

and

foreach ($xml -> Rate as $rowx){
$Symbol = (string)$rowx -> Symbol;
$Bid = (real)$rowx -> Bid;
$Ask = (real)$rowx -> Ask;
$Direction = (int)$rowx -> Direction;

It works up until this point, as when I echo the results, I get the 1st rows 4 columns, then the 2nd rows 4 columns, then the 3rd etc. right the way down to the last record on the site.

However, whenever I try to write this data to my MySQL table, i get 63 "MySQL Error" messages echo'd to me. My possible thoughts are that I've parsed it wrong, my host doesn't allow certain things (Although I can write data to databases with other PHP scripts), or I've structured it wrong.

Here is the full code, thanks to anyone who helps :) :

<?php

$url ="http://rates.fxcm.com/RatesXML3";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);


$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);



$con = mysqli_connect("localhost","Username","Password","Database") or die(mysqli_error($con));

foreach ($xml -> Rate as $rowx){
    $Symbol = (string)$rowx -> Symbol;
    $Bid = (real)$rowx -> Bid;
    $Ask = (real)$rowx -> Ask;
    $Direction = (int)$rowx -> Direction;

$sql = "INSERT INTO 'FXCM_Rates' ('Symbol', 'Bid', 'Ask', 'Direction') VALUES ('$Symbol', '$Bid', '$Ask', '$Direction')";

$result = mysqli_query($con,$sql);
if (!$result) {
    echo 'MySQL ERROR';
    } else{
    echo 'SUCCESS';
    }

}
?>
  • 写回答

1条回答 默认 最新

  • drd0833 2015-10-28 10:00
    关注

    Instead of quotes you have to use backtick in column and table name

     $sql = "INSERT INTO `FXCM_Rates` (`Symbol`, `Bid`, `Ask`, `Direction`) VALUES ('".$Symbol."', '".$Bid."', '".$Ask."', '".$Direction."')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟