Jump to content

storing rss feeds in mysql


proning

Recommended Posts

Hi im trying to create a rss read function which will read an rss news feed and store it in a mysql database so it can be formated and sorted.

 

i have managed to read the rss and save to the database but i want to prevent duplicate data entrys every time the page is loaded

heres my code so far

 

<?php
//function to take feed url and obtain rss entries
function newsfeed($feedURL)  {
$rss = simplexml_load_file($feedURL);
$i = 0;
$z = 0;


foreach ($rss->channel->item as $feedItem) {
	$i++;

	//split and store rss structure to varibles
	$feedTitle = $feedItem->title;
	$feedLink = $feedItem->link;
	$feedDescription = $feedItem->description;		
	$feedDate = ($feedItem->pubDate);
	$dateForm = explode(" ", $feedDate);


	//select db and load news to check for duplicate entrys
	$news = mysql_query("SELECT * FROM rssnews");
	$fnews = mysql_fetch_array($news);

	//test db entry
	echo $news['Title']; 

	//if database already contasins news then do not save	
	if ($feedTitle == $news['Title']){
		// do nothing if matches
		echo "<h1>News Not updated in database</h1>";
	}else {
		//insert new news to DB if not already on DB		
		$query = "INSERT INTO rssnews (Title, Link, Description, Date1, Date2, Date3, Date4, Date5) VALUES ('$feedTitle', '$feedLink', '$feedDescription', '$dateForm[1]', '$dateForm[2]', '$dateForm[3]', '$dateForm[4]', '$dateForm[5]')";
		mysql_query($query);

		echo "<p> -" . $feedTitle . " - " . $feedLink . " - " . $dateForm[1] . "- </p>";
		}
if($i >=5) break;
}
}


?>

 

all help is appreciated im pretty much a php novice so sorry in advance

 

 

:)

Link to comment
https://forums.phpfreaks.com/topic/238636-storing-rss-feeds-in-mysql/
Share on other sites

Did you mean that if you make an insert and u hit refresh in browser it inserts it again or something else? If you meant that the header location will work after the insert. If you did not mean it.. then I misunderstood and don't know really what you meant.

Doing the checks as you describe would require to do a mysql query and compare values of new content versus stored content, depending on if they match or not can do nothing/update....else insert new.

 

The above process would slow everything down, it may be better to just make a script that cycles through your database and deletes any similar stored content......meaning duplicate entries.

so ive managed to stop duplicate entrys by setting a the url to a unique key in the mysql table how ever there are 10 sets of news in the rss which i can echo out and render to the site but it is only entering 8 to the db

 

heres my code

 

<?php

//function to take feed url and obtain rss entries
function newsfeed($feedURL)  {
$rss = simplexml_load_file($feedURL);
$i = 0;

foreach ($rss->channel->item as $feedItem) {
	$i++;

	//split and store rss structure to varibles
	$feedTitle = $feedItem->title;
	$feedLink = $feedItem->link;
	$feedDescription = $feedItem->description;		
	$feedDate = ($feedItem->pubDate);
	$dateForm = explode(" ", $feedDate);			

	//insert new news to DB 	
	$query = "INSERT INTO rssnews (Title, Link, Description, Date1, Date2, Date3, Date4, Date5) VALUES ('$feedTitle', '$feedLink', '$feedDescription', '$dateForm[1]', '$dateForm[2]', '$dateForm[3]', '$dateForm[4]', '$dateForm[5]')";
	mysql_query($query);

	// test the lines entered into DB
	echo $feedLink;
	echo $feedTitle;
	echo $feedDescription;
	echo '<br /> <br />'; 

if($i >=200) break;
}
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.