Sep 10,  PST

How to Generate an RSS Feed using PHP & MySQL
Written by: Haily | Learn how to easily create an RSS Feed for your website using HTML and PHP.
 Facebook Google Buzz Mxx Digg StumbleUpon  

RSS IconOne of the most effective ways to keep your visitors returning to your website is to allow them to subscribe to your updates. It is for this reason that social media such as Facebook and Twitter has become so popular among businesses, brands, and websites small and large alike. But don't let the rise of social media cloud what was there before social media was an option: RSS. In fact, RSS Feeds may not be what help you promote your website, but it is by far the most effective way to have your users subscribe to your content and your website's updates. Most if not all CMSs (content management systems) or news scripts come with RSS Feeds. But what if it doesn't? Or what if you created your own system? (like I have) Or What if you want to create your own custom RSS Feed? Well, in this post I aim to show you how using PHP & MySQL.

Generating the Skeleton of the Feed

Before getting into the PHP part of the code, you must first create the skeleton of the feed, the feed properties, title, description, website link, etc. To make things simple you can start with this. At the end of the article, I will cover more about adding "finishing touches" to your feed. We'll start with this:

Code:
<?php
header('Content-Type: application/xml');
echo'<?xml version="1.0"?>';
?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>FEED TITLE</title>
<link>http://www.yourwebsiteurl.com</link>
<description>FEED URL</description>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>FEED GENERATOR(you can put your site here, I guess)</generator>

<atom:link href="http://www.yourwebsiteurl.com/feed.php" rel="self" type="application/rss+xml" />



</channel>
</rss>

It's very important to note here that the <atom:link> tag should have the URL of the feed. It may seem redundant, but there are applications that use this for better efficiency in updating the feed.

Adding Items to the Feed

Here's where we use PHP to call up the feed content from a MySQL database. First remember to connect to the database at the beginning of the code and to disconnect it at the end of the code. Next, we'll use a simply query to call up the necessary information. You'll modify the database info and the query to match your needs.

Code:
<?php
$dbhost='localhost';
$dbuser='USERNAME';
$dbpass='PASSWORD';
$dbname='DATABASE';
$conn=mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to $dbhost");
mysql_select_db($dbname) or die("Cannot select $dbname");

header('Content-Type: application/xml');
echo'<?xml version="1.0"?>';
?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>FEED TITLE</title>
<link>http://www.yourwebsiteurl.com</link>
<description>FEED URL</description>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>FEED GENERATOR(you can put your site here, I guess)</generator>
<atom:link href="http://www.yourwebsiteurl.com/feed.php" rel="self" type="application/rss+xml" />

<?php
$query = "SELECT title, time, author, post, alturl, cat FROM comm_news ORDER BY time DESC LIMIT 0, 10";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo'
<item>
<title>',htmlspecialchars($row['title']),'</title>
<link>',$newsurl,'</link>
<description><![CDATA[',$row['post'],']]></description>
<pubDate>',date("D, d M Y H:i:s",$row['time']),' -0700</pubDate>
</item>
';
}
?>

</channel>
</rss><?php mysql_close($conn); ?>

Conclusion - Adding Finishing Touches

There you go! Once you alter the specifics, you should get this to generate an RSS Feed for your site. Now if you would like to add some more to your feed or would like to learn what a feed can do, then I highly recommend you go through W3Schools RSS Tutorial. Using the navigation on the left, you can see the different types of tags that are available to you and how you can implement. These are just bells and whistles, the basic feed (above) is really all you need, but it's nice to have a well-developed feed as well.

 Facebook Google Buzz Mxx Digg StumbleUpon  

| Written by: Haily | Added: Mar 26 2010 | Last Modified: Mar 27 2010 | Views: 732 | (Log in to rate) |

User Comments

Jenny of windymill.skyne...March 27 2010, 3:17 am PST - Karma: 0 - Quote - Link -
4.5/5
haha this way more specific and easier to understand than similar articles on the net. Well done!

You are not currently logged into your IceCaves account. Log in?

Username: Password: | Create an account 

If you would prefer, you can post as a guest below:

Name:
Email: (required; for identification purposes; will not be published)
Site URL: (optional)
Rate:You must log in in order to rate.
Post:
BBCode?

Smilies:
Human Check:

Username:

Password:


Arid SeasVintaged.org

The IceCaves.net Topsites


Enchant Me Not | Unloadeed | Tugboat | Rabidish | Dash of Color | Starlett | NeopetsGuide | Figmint | Neo Crave | Rogue City | Spicy-Sugar | FGN-Guild.com | RainbowBliss | KeliJo.net | NeoWishes