PHP-XML到MySQL
我正在编写一个PHP代码,可以从XML中读取数据并将其存储在MySQL中。到目前为止,我已经到了从XML文件中读取数据并在网站上回显的地步。代码如下: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
//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_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());