PDA

View Full Version : How to judge RSS source, if there has new then UPDATE into database?



youlichika
13 Jan 2011, 05:54 PM
I have ordered yahoo news rss, I use simplepie to separate the news items like title, content, url, date... and then insert them into database. I catch the source every 2 hours with cron. How to judge RSS source, if there has new then UPDATE into database?

I mean if the rss source has 10 items, 5 are old, 5 are new, I want just update the 5 items new and ignore the 5 old items. I do not want insert the repeart items.
And I want to get `echo` a list of the 5 new items.

This is my insert code.


mysql_select_db("rss",$db1);
mysql_query("INSERT INTO yahoonews (link, title, date, content, image, imagelink) VALUES ('".$link."', '".$title."', '".$date."', '".$content."', '".$image."', '".$imagelink."')");

Alan
13 Jan 2011, 08:27 PM
There are a few ways you can do it. The publication date tag is optional but yahoo news seems to implement it. You can use that as you're timestamp. Read the RSS document, parse and insert into your database. When you go to read it again, get the timestamp of the last record, and don't store items before that timestamp. I didn't check to see if the feed was ordered, but ensure you insert records into the database in a proper order (by timestamp).

Also, don't just store the timestamp in the RSS feed. Parse it and store it as a DATETIME type. This will give you more control in your queries, making them easier to write.