0
我想插入XML數據到MySQL數據庫。我遇到的問題是每個標籤中都有多個id,因此我的腳本只加載第一個標籤。我如何重新編寫我的腳本來加載所有的ID到數據庫,每行一個ID。插入簡單的XML列表到mySQL
<?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>
?>
你爲什麼要調用'new SimpleXMLElement()'兩次?同樣,從數據看來,您需要''foreach'' ids'而不是'id_list',例如:'foreach($ twelement-> id_list-> id爲$ data)''id_list'實際上只有一個元素,而不是包含id的'id'元素。 – Yaniro 2011-12-31 17:07:11
當我像上面的foreach($ twelement-> id_list-> id爲$ data)一樣編輯foreach時,出現錯誤,插入語句是否正確? – WAUS 2012-01-07 03:57:54