在mysql数据库中存储rss提要的一些问题


Some issues with the storing of rss feeds in the mysql database

到目前为止,我通过以下代码(rss store .php)或多或少成功地将来自不同网站的rss提要(基于xml的文档)解析到我的MySQL数据库中:

    <?php
header("Content-Type: text/html;charset=utf-8");
$feeds = array(
    'http://fotbollskanalen.se/rss',
    'http://www.aftonbladet.se/sportbladet/fotboll/rss.xml',
    'http://www.eurosport.se/fotboll/allsvenskan/rss.xml',
    'http://expressen.se/rss/fotboll'
);
$connect=mysql_connect("localhost","root","") or die("You could not access!");
mysql_select_db("storage",$connect);
if($connect)
{
foreach( $feeds as $feed ) {
    $xml = simplexml_load_file($feed);
    foreach($xml->channel->item as $item)
    {
    $date_format = "j-n-Y"; // 7-7-2008
    echo date($date_format,strtotime($item->pubDate));  
             echo ' <a href="'.$item->link.'" target="_blank">'.$item->title.'</a>';
             echo '<div>' . $item->description . '</div>';
    mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate) 
    VALUES (
        '', 
        '".mysql_real_escape_string($item->title)."', 
        '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
        '".mysql_real_escape_string($item->link)."', 
        '".mysql_real_escape_string($item->pubdate)."')");       
    }
}
}
else
{echo "An error has occured";}
mysql_close($connect);
?>

作为结果,它显示了来自4个链接的rss提要,当我驱动这个脚本作为测试时,我也看到它响应服务器上的数据库,因为有文件插入到mysql表中。

但是现在问题来了。以下是我需要帮助的问题:

1:当我在数据库上查看mysql表"rss_feeds"时,我看到很多奇怪的字母,似乎已经取代了瑞典字母"Å","Ä"answers"Ö",我认为它可能是mysql表resp行上的字符集。数据库本身。但如果是这样,那么我该如何改变它来解决这个问题呢?

2:虽然MySQL数据库"storage"响应了"rss store . PHP"中的PHP代码,但自从数据库和PHP文件相互连接以来,我没有看到新的rss提要被插入。我是否应该在"rss store。PHP "中添加另一个PHP代码,或者我应该怎么做才能将最新的rss提要插入到数据库中?

3:当涉及到数据库上RSS提要的ID时,它们都显示为0。如何使它们唯一,每个RSS提要都有不同的ID?

4:当我在数据库"storage"上的"rss_feeds"表中查看RSS提要的发布日期时,它们都显示为"0000-00-00 00:00:00"。为什么会这样呢?下面的代码位于数据库中的pubdate行之前:

5       pubdate timestamp   on update CURRENT_TIMESTAMP   Null: No  CURRENT_TIMESTAMP   ON UPDATE CURRENT_TIMESTAMP
这是最后一个问题,但可能是最重要的。我想在数据库"存储"上的mysql表"rss_feeds"中设置一个关于插入rss提要的最大限制,这样rss提要就不会过多地增长,并且以某种方式威胁到整个本地服务器,如果你明白我的意思。我是否应该添加一些SQL代码来决定限制或者应该如何做?

如果你需要一些额外的必要信息来帮助我解决这些问题,那么我将按照你的意愿提供更多的信息。

我将向那些帮助我度过所有这些问题的人致以最良好的问候:D

提前感谢!

使用此函数

$ feed -> set_output_encoding("iso - 8859 - 1");

这将确保RSS提要的输出与MySQL编码匹配。否则你最终会得到像€这样的字符。来源:http://samueljcarlson.blogspot.in/2011/12/rss-feeds-to-mysql-database-script.html