从RSS获取数据并插入MYSQL

时间:2012-04-16 00:20:54

标签: php mysql arrays insert rss

我有一个RSS Feed,其中的项目包含标题和说明。我想将它们加载到表中

我有这样的东西,但它不会回应任何东西或插入任何东西。

 $con = mysql_connect("localhost","USERNAME","PASSWORD");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }





$rss = simplexml_load_file('rss.xml');

foreach ($rss->item as $eachFilm) {
    $film_name = array($eachFilm->title);
    $film_release = array($eachFilm->description);
    echo $film_name;

    mysql_query("INSERT INTO Films (Film_Name, Film_Release)
VALUES ('$film_name', '$film_release')");

}

mysql_close($con);

非常感谢任何帮助!

编辑 - 示例rss -

</image><item>
             <title>Remembrance</title>
             <link>http://www.filmdates.co.uk/films/3950-remembrance/</link>
             <description><![CDATA[Release Date: Wednesday 18th April 2012]]></description>
             <category>Movie</category>
             <pubDate>Tue, 27 March 2012 20:31:29 MST</pubDate>
           </item><item>
             <title>In Search of Haydn</title>
             <link>http://www.filmdates.co.uk/films/3672-in-search-of-haydn/</link>
             <description><![CDATA[Release Date: Thursday 19th April 2012]]></description>
             <category>Movie</category>
             <pubDate>Mon, 16 April 2012 19:33:41 MST</pubDate>
           </item><item>

1 个答案:

答案 0 :(得分:0)

我不知道你的rss文件,你的Rss文件的完整结构是,

这是一个带有simplexml_load_file

的wordpress rss文件的快速示例
$conn = mysql_connect("localhost", "*****", "****");
if($conn)
    mysql_select_db("test", $conn);

$rss = simplexml_load_file('file.xml');
$getChild = $rss->children();

foreach( $rss->children() as $item ){

    // Count Total Items in the Files
    $total_items = $item->item->count();
    for( $i=0; $i < $total_items; $i++ ){
        $title = $item->item[$i]->title;

        $query = mysql_query("insert into tbl_name (name) value ('$title')");
        if( $query ){
            echo "Inserted Success! <br>";
        }
        else{
            echo "Error in Query!<br>";
        }
    }
}

以上示例将与wordpress xml文件一起使用,我已经在我的本地测试并且完美地工作。希望这会对你有所帮助。