Creating RSS feed in xml format with PHP and SQL

RSS (Rich Site Summary) is an important feature for online publishers and readers as it provides platform to get frequently updated content like blogs, news, articles, audio, video so the readers do not have to check site for updates. If you own a website where content is updated frequently then you must have RSS feed enabled so your readers get updated frequently.

There are many tools available to generate static rss feed but for dynamic sites that is not suggested. Many blog platforms like WordPress and Blogger provide this feature built in but you can create your xml feed easily with PHP and SQL.

Here is the example of rss feed xml format.
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
    <title>RSS Title</title>
    <description>This is an example of an RSS feed</description>
    <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
    <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
    	<title>Example entry</title>
    	<description>Blog description.</description>
	    <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>
Now we will create this xml file with PHP and SQL
Create a table where you will add the title, description, publish date and URL of your blog or article.

title TEXT,
description TEXT,
date datetime,
url TEXT,
Every time you publish your content add the required fields in this table. Now we will create a PHP file where we will write the code to create xml from this table.
$path = "provide path to store xml";
$br  = '\n';
$tab = '\t';
$xml = '<?xml version="1.0" encoding="UTF-8"?>'.$br;
$xml.= $tab.'<channel>'.$br;
$xml.= $tab.$tab.'<title>Your Feed Title</title>'.$br;
$xml.= $tab.$tab.'<description>Description for your feed</description>'.$br;
$xml.= $tab.$tab.'<link>'.$path.'</link>'.$br;
$xml.= $tab.$tab.'<lastBuildDate>'.date("r").'</lastBuildDate>'.$br;

$sql = mysqli_query("select * from rssFeed ");
 while($row = mysqli_fetch_array($sql))
	$date = date("D, d M Y H:i:s O", strtotime($row['date']));
	$xml.= $tab.'<item>'.$br;
	$xml.= $tab.$tab.'<title>'.$row['title'].'</title>'.$br;
	$xml.= $tab.$tab.'<description>'.trim($row['description']).'</description>'.$br;
	$xml.= $tab.$tab.'<link>Your-site-address/'.$row['url'].'</link>'.$br;
	$xml.= $tab.$tab.'<pubDate>'.$date.'</pubDate>'.$br;
	$xml.= $tab.'</item>'.$br;

$xml.= $tab.'</channel>'.$br;
$xml.= '</rss>';
$handle = fopen($path.'feed.xml','w+');

when ever you write new blog or article the run this file which will update the feed.xml file.

In $br variable add \n for new line and add \t for tabs in $t this will create your xml properly formated

In $path provide the where you want to store the feed.xml. remember to always use the date format which is provided else your feed will not be verified. You can validate your feed at

Once your file is created you can add this to feed directories which will help you distribute your feed to the readers and can get you more audience.

Recommended: Adding RSS Feed to feedburner for email subscription

Tags: rss feed
comments powered by Disqus