This post explains how to create RSS feed for web projects with PHP. It's simple code just you have to include the basic RSS stucture in while loop.

Basic structure of RSS feed.
<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>Website Title </title>
<link>Website URL</link>
<description>Website description</description>
<language>en-us</language>
<item>
<title>Item/Article Title</title>
<link>Item/Article URL </link>
<description>Items/Article description </description>
</item>
</channel>
</rss>
<rss version='2.0'>
<channel>
<title>Website Title </title>
<link>Website URL</link>
<description>Website description</description>
<language>en-us</language>
<item>
<title>Item/Article Title</title>
<link>Item/Article URL </link>
<description>Items/Article description </description>
</item>
</channel>
</rss>
Sample Database Table.
CREATE TABLE site_data
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(250),
link VARCHAR(250),
description TEXT
);
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(250),
link VARCHAR(250),
description TEXT
);
rss.php
Code contains PHP and XML RSS basic structure tags. So your feed link http://domain.com/rss.php.
<?php
include('db.php');$sql = "SELECT * FROM site_data ORDER BY id DESC LIMIT 20";
$query = mysql_query($sql) or die(mysql_error());
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>9lessons | Programming Blog </title>
<link>http://9lessons.blogspot.com</link>
<description>Programming Blog </description>
<language>en-us</language>"; while($row = mysql_fetch_array($query))
{
$title=$row['title'];
$link=$row['link'];
$description=$row['description'];
echo "<item>
<title>$title</title>
<link>$link</link>
<description>$description</description>
</item>"; }
echo "</channel></rss>";
?>
Related Posts :
Reading and Displaying RSS Feed with PHP.















I forgot exactly how, but you can also have PHP change the document header to xml instead of whatever the default is. This is useful when you want an rss reader to detect that a file is xml instead of html.
Generating XML in this way is an awful idea. There's XML libraries for a reason.
You're not doing an escaping either.
I agree with Ian, you need to escape your title and description of each item. You can use htmlentities or htmlspecialchars for that.
hi ,
This gives me a warning Error headers cannot modify ........
i have a problem with tamil feed generation. Please can you help me?
nelamurugan@gmail.com
tamada bujji really nice demos ....ramesh & ravi
you can add rss+xml to display the data as rss feed as below:-
@header("Content-type: text/rss+xml");
Great article, thanks!
this works..but nit teh best way :P
Is it allowed to use another encoding in rss feed instead of utf-8?