954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

RSS file generated from DB table, Automatically? Any tips?

Hi.

I need to create an RSS file from one table in my database. I'm using PHP5 and MySQL.

I have searched on this and tried to do it myself but i'm not sure what path to take. Should I use SimpleXML to do it?

I am hoping someone might be able to point me in the right general direction. So anyone done this before and can help me out?


Many thanks.

rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
 

You can get PHP code (class) to gerenrate RSS from

http://forums.digitalpoint.com/showthread.php?t=32265 .

Retrieve records from your mysql table, and use above class to generate RSS.

you can dump the output as .xml file .. or an php script can return a rss as well.

RichAppsConsult
Newbie Poster
2 posts since Sep 2008
Reputation Points: 10
Solved Threads: 2
 

Hi RichAppsConsult,

Thanks for the page. I had actually come across that one when searching. I didn't use it as there seems to be bits that I don't need in it and I don't enderstand everything it uses such as the '' namespace use.

I'm looking to just output 4 or 5 text fields from my DB (story_title, story, link, link_title, date) to a simple XML (in RSS format) file.

I don't need images etc and would love to keep it simple at first and just make sure I understand everything.

I see lots of stuff for parsing XML files and I can actually already do that OK but it is not as easy to find something coherent on the issue of outputting XML.

rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
 

Since the XML output for an RSS is fairly static, I use smarty to generate it, instead of an XML parser.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi,

Thanks for the two replies.

I just got it working as the second comment came in. I thought would post up the code for others.

Basically it connects to a table and keeps adding text to the $xml_output variable and then writes what is in the variable to an xml file.

In it's current state I am still trying to get it to validate but it worked for me so should get you going.

<?php
$host = "localhost";
$user = "username";
$pass = "password";
$database = "name of DB";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM updates ORDER BY id DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_output .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
$xml_output .= "<channel>\n";
$xml_output .= "<title>Title of whole feed</title>\n";
$xml_output .= "<link>link for whole feed</link>\n";
$xml_output .= "<description>Description of whole feed</description>\n";
$xml_output .= "<language>en</language>\n";

for($x = 0; $x < mysql_num_rows($resultID); $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "\t<item>\n";
    $xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
    $xml_output .= "\t\t<link>" . $row['link_dest'] . "</link>\n";
    $xml_output .= "\t\t<description>" . $row['description'] . "</description>\n";
	$xml_output .= "\t\t<guid isPermaLink=\"true\">http://www.yoursite.com#".$row['id']."</guid>\n";
    $xml_output .= "\t</item>\n";
}

$xml_output .= "</channel>\n";
$xml_output .= "</rss>";

$filename_path = '../feeds/filename_'.$_COOKIE['lang'].'.xml';

$file_write = file_put_contents($filename_path, $xml_output);



?>
rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You