05 January 2011

How to create dynamic rss feed using php mysql

Dynamic rss feed can be created in the similar manner as static rss feed, but it will be saved in a php file (eg. Rss.php ). In dynamic rss feed first we define the content type of the script and then we write xml script exactly same to the static feed except that the item tag is now written inside a while loop which is fetching data from the database.

Let’s make an example dynamic rss feed to give you guys a clear picture of what we are talking about. To make this example simple I will not be including the database connection code in this script.

Syntax

First we define the content type of the script as xml and rss. This can be done using the following line of code:
<?php header("Content-Type: application/rss+xml; charset=ISO-8859-1"); ?>
After defining the content type we will initialize the rss feed by placing following xml code:
<?php echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
Now we need to define information about our site. Place the following code immediately after the previous code in your xml file:
<title>Skills 2 Earn</title>
<description>Knowledge for all</description>
<link>http://skills2earn.com</link>
In the above code “<title>” tag is used for setting the title of your site, “<description>” tag for description and “<link>” tag for the link of your site.

Now comes the most important part of your dynamic rss feed, in this part you need to define the information about your content. For this purpose “<item>” tags are used. If you want to put four topics in your feed then you need to use four “<item>” tags. We will do it by defining “<item>” tags inside a while loop. Following is an example code to let you know how these item tags can be used:
<?php
$sql="SELECT title,description,link FROM table1 LIMIT 5";
$result=mysql_query($sql) or die ($sql."<br />".mysql_error());
while ($row=mysql_fetch_array($result))
{
?>
   <item>
   <title><?php echo $row[‘title’]; ?></title>
   <description><?php echo $row[‘description’]; ?></description>
   <link><?php echo $row[‘link’]; ?></link>
   </item>
<?php
}
?>
In the above code “<title>” tag is used for setting the title of your topic, “<description>” tag for description and “<link>” tag for the link of your topic.

In the end you need to close all open tags. As I am doing in the code below:
</channel>
</rss>
That’s it guys, you have done it, you have created dynamic rss feed using php and mysql, but remember this example code is not supposed to work as it’s a dummy code just to make you guys understand how a dynamic rss feed is created.

Complete Code

Complete code is mentioned below. You need to safe it in a php file and you can name it as “rss.php”.
<?php header("Content-Type: application/rss+xml; charset=ISO-8859-1"); ?>
<?php echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Skills 2 Earn</title>
<description>Knowledge for all</description>
<link>http://skills2earn.com</link>
<?php
$sql="SELECT title,description,link FROM table1 LIMIT 5";
$result=mysql_query($sql) or die ($sql."<br />".mysql_error());
while ($row=mysql_fetch_array($result))
{
?>
   <item>
   <title><?php echo $row[‘title’]; ?></title>
   <description><?php echo $row[‘description’]; ?></description>
   <link><?php echo $row[‘link’]; ?></link>
   </item>
<?php
}
?>
</channel>
</rss>
For any problems or queries, you may have, feel free to comment on this post. I would love to resolve your problems.

4 comments:

sylvesta said...

I'm looking to have the rss feed automatically take the first paragraph of each page of content that I add to the site automatically with a [read more] attribute at the end. Will this code do that?

Sonia said...

how to create search result rss... i mean if one wish to subscribe to a particular search result

phponwebsites said...

Thank you very much

phponwebsites said...

Thanks...