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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器