PHP-XML到MySQL

PHP-XML到MySQL,php,mysql,xml,Php,Mysql,Xml,我正在编写一个PHP代码,可以从XML中读取数据并将其存储在MySQL中。到目前为止,我已经到了从XML文件中读取数据并在网站上回显的地步。代码如下: <?php //mysql connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("bet_sql") or die(mysql_error()); $xml = simplexml_load_file('http

我正在编写一个PHP代码,可以从XML中读取数据并将其存储在MySQL中。到目前为止,我已经到了从XML文件中读取数据并在网站上回显的地步。代码如下:

<?php

//mysql connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("bet_sql") or die(mysql_error());

$xml = simplexml_load_file('http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=46&marketSort=MR&filterBIR=N');

foreach ($xml->response->williamhill->class->type as $type) {
  $type_attrib = $type->attributes();
  $type_attrib['id'];
  $type_attrib['name'];
  foreach ($type->market as $event) {
    $event_attrib = $event->attributes();
    $event_attrib['id'];
    $event_attrib['name'];
    $event_attrib['date'];
    $event_attrib['url'];
    foreach ($event->participant as $participant) {
        $participant_attrib = $participant->attributes();
          $participant_attrib['name'];
          $participant_attrib['oddsDecimal'];
    }
  }

  mysql_query("INSERT INTO games (type_id, type_name, event_id, event_name, event_url, participant_name, participant_odds) 
          VALUES ($type_attrib[id], $type_attrib[name], $event_attrib[id], $event_attrib[name], $event_attrib[url], $participant_attrib[name], $participant_attrib[oddsDecimal]) ")
          or die(mysql_error());
}
?>

mysql\u查询有什么问题?我收到这个信息:

您的SQL语法有错误;查看与MySQL服务器版本相对应的手册,了解第2行“Jupiler League,134465843,Zulte Waregem v Genk-75分钟下注”附近使用的正确语法


谢谢你的帮助

这是一个很好的例子,说明了为什么应该使用。它不仅比反复运行相同的
INSERT
语句更快,而且可以避免转义问题,让您摆脱过时的
mysql\u查询
函数

我不得不猜测数据类型的用途


插入查询的问题是

  • 数组键不带单引号,这将导致警告
  • 值未正确转义。这可能是语法错误的主要原因

    mysql_query("INSERT INTO games (type_id, type_name, event_id, event_name, 
    event_url, participant_name, participant_odds) 
    VALUES ($type_attrib['id'], $type_attrib['name'], $event_attrib['id'], 
    $event_attrib['name'], $event_attrib['url'], $participant_attrib['name'],
    $participant_attrib['oddsDecimal'])"
    ) or die(mysql_error());
    

请注意,因为我看到您正在处理体育书籍数据。如果您遇到此类错误的问题,您必须是MySQL的新手,可能不应该在没有人监督的情况下在生产环境中处理这些数据。您在查询中没有引用数据,您应该使用PDO或MySQLi,你应该给出一个例子,让这成为一个有用的答案^^^你能帮我更多一点准备好的陈述吗?在我的情况下怎么做?如果没有,感谢您提供的资源!增加了一个例子。它的缩写是为了演示,我没有放任何代码,它与问题中指定的查询相同。以上两点仅供参考和支持。
mysql_query("INSERT INTO games (type_id, type_name, event_id, event_name, 
event_url, participant_name, participant_odds) 
VALUES ($type_attrib['id'], $type_attrib['name'], $event_attrib['id'], 
$event_attrib['name'], $event_attrib['url'], $participant_attrib['name'],
$participant_attrib['oddsDecimal'])"
) or die(mysql_error());