I’m trying to insert XML data into mysql database. Issue I’m having is that there are multiple id’s within each tag so my script loads the first only. How do I re-write my script to load all id’s to the db, one id per row.
<?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>
?>
Here’s how you should do it:
It’s not at all intuitive but the ids are hiding inside $xml->ids[ 0 ].
Your SQL statement seems fine.