RSS Feed any MySQL Database using PHP

To some folks rss is this hot new technology that is changing our lives in technology. The truth is rss is a specific formated text file that people finally have standarized on. It’s the standarization that changes people lives, not neccessary the technology. Anyway, a while back I wanted to rss feed our knowledgebase. The knowledgebase articles are stored in a mysql database. I played around with using PEAR::PACKAGE::XML to do the job but found it to be a bit overkill. So I wrote my own xml builder - source code is below or you can download the php rss feed mysql database code. What is nice, is that you can RSS feed any database.

// prepare HTML text for use as UTF-8 character data in XML
function cleanText($intext) {
return utf8_encode(
htmlspecialchars(
stripslashes($intext)));
}

// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=utf-8");

// retrieve database records

$db = mysql_connect("YourMySQLSERVER", "YOURUSERNAME", "YOURPASSWORD");
if (!$db)
{
error_log("Error: Could not connect to database in rss.php.");
exit;
}

// store items from the database in the $result1 array
mysql_select_db("knowledgebase");

$query1 = "SELECT faqarticles.question, faqarticles.content, SUBSTRING(content,1,600) as mycontent,
faqarticles.cat, faqarticles.id, faqarticles.date FROM faqarticles WHERE
faqarticles.approved != '1'
ORDER BY faqarticles.date DESC";
$result1 = mysql_query($query1);
$phpversion = phpversion();

// display RSS 2.0 channel information

ECHO <<

http://www.yourlinkgoeshere.com Describe your RSS Feed Heredescription>
en-us
http://backend.userland.com/rss
PHP/$phpversion


http://www.yoursite.com/images/YourLogo.gif

http://www.yoursite.com/index.php 140
60
Describe Your Image

END;

// loop through the array pulling database fields for each item
for ($i = 0; $i < mysql_num_rows($result1); $i++) {
@$row = mysql_fetch_array($result1);
$title = cleanText($row["question"]);
$link = "http://www.YourSite.com/kb/index.php?article=".$row["id"];
$description = $row["mycontent"];

//Replace Ugly HTML that got into the Knowledgebase with nothing
$desc_replace = array("

 

“, “

 

“, );
$desc_replace_with = array(”", “”, “”);
$desc_temp = str_replace($desc_replace, $desc_replace_with, $description);

//Now clean the HTML
$mydescription = cleanText($desc_temp);
$pubDate = $row[”date”];

// display an item
ECHO <<

$link $mydescription… For the entire article, please visit our site.

$pubDate


END;

}

ECHO <<



END;

?>

Written by Tim on February 16th, 2006 with 9 comments.
Read more articles on php.



Related articles

9 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com Marc
#1. December 11th, 2007, at 8:02 AM.

Wish that code was still up.

Get your own gravatar by visiting gravatar.com cheap jerseys
#2. August 29th, 2010, at 2:58 PM.

hey buddy,this is one of the best posts that I’ve ever seen; you may include some more ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.

Get your own gravatar by visiting gravatar.com F540
#3. January 19th, 2011, at 1:58 AM.

It’s a very old post but your source code just gave me an idea ! Hehe, thanks !

Get your own gravatar by visiting gravatar.com Frisco Plumber
#4. January 19th, 2011, at 7:44 PM.

Great information. I got lucky and found your site from a random Google search. Fortunately for me, this topic just happens to be something that I’ve been trying to find more info on for research purpose. Keep us the great and thanks a lot.

Get your own gravatar by visiting gravatar.com Twin Cities Plastic Surgery
#5. February 1st, 2011, at 7:45 PM.

Great information you’ve provided us with here. Thanks so much for sharing. Nice site too

Have a great one

Get your own gravatar by visiting gravatar.com Iowa City Locksmith
#6. April 2nd, 2011, at 2:47 AM.

Great site you have here. I really enjoyed all of the insightful information you’ve provided us with and look forward to read more on this site in the future.

Great job

Get your own gravatar by visiting gravatar.com Rick
#7. April 16th, 2011, at 9:19 PM.

Very useful just what i needed. Thanks alot..

Get your own gravatar by visiting gravatar.com Rhiannon Deschene
#8. August 26th, 2011, at 5:52 PM.

Unquestionably consider that that you just reported. Your much-loved rationale have also been over the internet the easiest factor to know. I say to you, My partner and i definitely have irked even though persons contemplate problems that they will in plain english don’t understand concerning. You actually was able to click the particular claw when the best and also explained out the existing issue without necessity sideeffects , people today can take an indication. Are going to be here we are at find more. Appreciate it

Get your own gravatar by visiting gravatar.com Asics Gel Kinsei 2 II Shoes
#9. December 8th, 2011, at 6:44 AM.

I desired to thank you for this fascinating I undoubtedly loved just about every small little of it. I have you bookmarked your internet website to confirm out the newest stuff you post.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




Anti-Spam Protection by WP-SpamFree

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .

Famous Homemade Soups