RSS Feed from a MySQL Database

This tutorial will teach you how to create a simple RSS 2.0 compliant feed from a MySQL (or others with a few small changes) database.

The first thing you must do in a RSS document is to define that it is indeed an RSS feed. We do this like so…

echo '<?xml version="1.0" ?>';
echo '<rss version="2.0">';

We then must open the ‘channel’ tag, which is just our feeds main content and then give our feed some information of what it is and where it is from.

echo '<channel>'; // Opens our main content
echo '<title>Test Website RSS Feed</title>';    // Defines the title of the RSS feed
echo '<link>http://www.testsite.com</link>';    // Where the RSS feed is from
echo '<description>This RSS feed is all about the pie.</description>'; // What its about

We then open a MySQL connection and query the server for our information, I have used a database called “test” and a table named “news”. As you can guess, this RSS feed is going to show the latest news from our test website.

$c = mysqli_connect('localhost', 'user', 'pass', 'test') or die(mysqli_error($c)); // Connect with our information
$q = mysqli_query($c, 'SELECT * FROM news ORDER BY id DESC LIMIT 5') or die(mysqli_error($c)); // Query the table for 5 news articles sorting by 'id'
while($r = mysqli_fetch_assoc($q)) // While there is more rows to get (max of 5) we get an associative array
{

Now we want to print out each news article in a RSS compliant manner. All <item> elements (each news article) must have a title, link and description to be RSS compliant. I have added <author> and <pubDate> (published date) also. Note: <pubDate> must be in the RFC 822 standard.

echo '&lt;item&gt;'; // Begin a news article
echo '&lt;title&gt;'.$r['title'].'&lt;/title&gt;'; // Give it a title
echo '&lt;link&gt;'.$r['permalink'].'&lt;/link&gt;'; // The link to the article
echo '&lt;description&gt;'.$r['content'].'&lt;/description&gt;'; // The description or content of the article
echo '&lt;author&gt;'.$r['author'].'&lt;/author&gt;'; // The author
echo '&lt;pubDate&gt;'.$r['date'].'&lt;/pubDate&gt;'; // The date is was published
echo '&lt;/item&gt;'; // End news article
} // End while statement

We then finish the document by closing our <channel> and <rss> tags.

echo '&lt;/channel&gt;';
echo '&lt;/rss&gt;';

I hope you enjoyed my first tutorial, please comment if you have any questions.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Great tutorial Mike, thanks for your contribution :)

I remember when I first started out creating a “dynamic” feed for my website, many years ago. ‘Cause I thought the files had to end in “XML” I actually wrote to a file using my PHP script. Bad, bad!

Thank u very much!

Just making the jump from ASP to PHP and wanted to rewrite my old ASP/Access generated RSS feed, and this is a great helping hand.

@Adam

Writing to a file isn’t necessarily a bad thing. If you create a new feed file only when your content changes then you’ll be saving yourself a lot of unnecessary database calls, which is generally a good thing, particularly if you have a large number of feed subscribers.

Sam has right … anyway writing the code with sql is more simple than reading/writing from file.

BR

I was hoping you would make a tutorial for atom feeds. I’m not sure the big difference between atom and RSS.

Copied your code, and it dosent work properly

Oh my god. I surfed all over the internet after such an easy understanding guide to rss.

Really nice dude, keep up the good work.

Greetings from Danmark.

No way. I would use xmlWriter which is much easier to use.

http://www.php.net/xmlWriter

Thanks a lot ! :)

Thanks a lot for your tutorial ..you save a lot of my time…

Good tutorial!

My only question is how would you save this file? It would definitely be .php but how would the file be used as an actual RSS feed? I am trying to create a quick RSS feed for my news feature on my new site, and it requires the use of MySQL databases, so w00t. If I could get that one question answered, what to save the file as and how to implement it, then I will use your code! Thanks.

And to Bjoern, if you just straight up copied/pasted this guy’s code for your use, then I think you should go read a tutorial or two on a beginner’s guide to php, mysql, xml, and a few other codes that are implemented in this script. That’s all I will say.

=Alexander=

très bon travail, merci

(very good work, thanks)

Sorry. Can you help me use it for Wordpress? I can’t use it with my Blog. Test >>> http://vnnewbies.com/rs.xml.

Thank a lot.

Hey, Help Me

You need to study the wordpress database first and determine what fields need to be pulled for your blog/rss feed.

Then use the script to do it based on these fields.

I agree with Alex. How would you save this file?

Leave a comment

(required)

(required)