You are here:  » Entering RSS feed contents into a MySQL Database?


Entering RSS feed contents into a MySQL Database?

Submitted by raycropper on Fri, 2007-07-13 10:01 in

Hi There,

I am currently trying to integrate an RSS feed from my own site into another site and to that end I am attemtping to place the contents of the feed into a table in a MYSQL database so I can build up a searchable archive of news stories.

I have used the following code which only seems to insert 1 news item into the table, can anyone tell me how I can get the entire contents of the feed to the database? I have set the feed to contain 10 items of news so esentially

This is the code I have used so far:

<?php
  
require("MagicParser.php");
  
$link mysql_connect("localhost""xxxxxxxxxx""xxxxxxxxxx") or die('Could not connect: ' mysql_error());
  
mysql_select_db('xxxxxxxxxxxxxx') or die('Could not select database');
  function 
myRSSRecordHandler($item)
  {
    
$sql "INSERT INTO rss SET title = '".mysql_escape_string($item["TITLE"])."', link = '".mysql_escape_string($item["LINK"])."', description = '".mysql_escape_string($item["DESCRIPTION"])."',  pagebody = '".mysql_escape_string($item["PAGEBODY"])."', logo = '".mysql_escape_string($item["LOGO"])."', contactdetails = '".mysql_escape_string($item["CONTACTDETAILS"])."'";
    
mysql_query($sql);
  }
  
$url "http://edmundson-electrical.voltilink.co.uk/rss.jsp?partner=edmundson&password=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  
// now we call Magic Parser and let myRecordHandler load each item into the database
  
MagicParser_parse($url,"myRSSRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

Any help would be greatly appreciated.

Thanks,

Ray

Submitted by support on Fri, 2007-07-13 10:10

Hello Ray,

Your code essentially looks correct, which means the problem is almost certainly down to one of:

i) There is only 1 item being returned in the feed. To check this, printout some debug code so
that you can see what is going on - for example add the following immediately after the mysql_query():

    print "<p>".$item["TITLE"]."</p>";

You should then see the 10 items displayed, which will rule out this problem. Otherwise,

ii) There could be a key problem with the database (duplicate key for example). To find out what might
be happening here, add some database debug code, so where you currently have:

    mysql_query($sql);

...change this to:

    if (!mysql_query($sql))
    {
      print "<p>".mysql_error()."</p>";
    }

The error message displayed (if any) should help identify the cause of the problem...

Hope this helps!
Cheers,
David.

Submitted by raycropper on Fri, 2007-07-13 10:53

I didn't auto-increment the primary key, seemed to be causing the problem. Thanks very much for your help

You are a gentleman and a scholar!!

Thanks,

Ray