Php 将简单XML列表插入mySQL
我正在尝试将XML数据插入mysql数据库。我遇到的问题是,每个标记中都有多个id,所以我的脚本只加载第一个id。如何重新编写脚本以将所有id加载到db,每行一个idPhp 将简单XML列表插入mySQL,php,mysql,xml,insert,Php,Mysql,Xml,Insert,我正在尝试将XML数据插入mysql数据库。我遇到的问题是,每个标记中都有多个id,所以我的脚本只加载第一个id。如何重新编写脚本以将所有id加载到db,每行一个id <?php require_once 'db-functions.inc.php' ; //custom database functions $xmldata = 'http://api.twitter.com/1/followers/ids.xml?cursor=-1&screen_name=aplusk
<?php
require_once 'db-functions.inc.php' ; //custom database functions
$xmldata = 'http://api.twitter.com/1/followers/ids.xml?cursor=-1&screen_name=aplusk';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);
foreach ($xml->ids as $data)
{
$id = $data->id;
mysql_query("INSERT INTO data (id)
VALUES ('$id')");
};
// sample of xml I want to insert
// <id_list>
// <ids>
// <id>275168965</id>
// <id>28245852</id>
// <id>15112249</id>
// </ids>
// <next_cursor>0</next_cursor>
// <previous_cursor>0</previous_cursor>
// </id_list>
?>
以下是您应该如何做到这一点:
$xml = new SimpleXMLElement( file_get_contents( 'http://api.twitter.com/1/followers/ids.xml?cursor=-1&screen_name=aplusk' ) );
foreach ( $xml->ids[ 0 ] as $data )
{
echo( $data . "<br>" );
}
这一点都不直观,但ID隐藏在$xml->ids[0]中。
您的SQL语句似乎很好。为什么要调用两次新的SimpleXMLElement?而且,从数据上看,您需要foreach ids元素而不是id\u列表,例如:foreach$twelement->id\u list->ids as$data实际上id\u列表中只有一个元素,而不是包含id的ids元素。当我按上述foreach$twelement->id\u list->ids as$data编辑foreach时,我得到一个错误,insert语句正确吗?