How to create rss feeds using php and mysql

In this post i want to explain how to create rss feeds using php code and get the data from mysql table. It is very simple to develop and use.

Step 1 : Connect to database

We already discussed about how to connect database using php code.See the following code to connect database:


DEFINE ('DB_USER', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'rss');

// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

By using about code we can connect to database sucessfully.


class RSS
public function RSS()
require_once ('mysql_connect.php');

public function GetFeed()
return $this->getDetails() . $this->getItems();

private function dbConnect()

private function getDetails()
$detailsTable = "webref_rss_details";
$query = "SELECT * FROM ". $detailsTable;
$result = mysql_db_query (DB_NAME, $query, LINK);

while($row = mysql_fetch_array($result))
$details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
. $row['title'] .'</title>
. $row['link'] .'</link>
. $row['description'] .'</description>
. $row['language'] .'</language>
. $row['image_title'] .'</title>
. $row['image_url'] .'</url>
. $row['image_link'] .'</link>
. $row['image_width'] .'</width>
. $row['image_height'] .'</height>
return $details;

private function getItems()
$itemsTable = "webref_rss_items";
$query = "SELECT * FROM ". $itemsTable;
$result = mysql_db_query (DB_NAME, $query, LINK);
$items = '';
while($row = mysql_fetch_array($result))
$items .= '<item>
. $row["title"] .'</title>
. $row["link"] .'</link>
. $row["description"] .']]></description>
$items .= '</channel>
return $items;



By using about code we can create a class for rss feed.

Step3: Create a object to class and display rss feeds.

header("Content-Type: application/xml; charset=ISO-8859-1");
$rss = new RSS();
echo $rss->GetFeed();

Hope that it will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

7 Responses

  1. Suresh says:

    It’s really looking great. If possible very soon i’ll give it a try…

  2. shashank says:

    deinfitely would give this a try..
    thnx bro
    gr8 sharE

  3. wweb says:

    Wow that’s cool. I will for sure try this 🙂 Thanks Anil

  4. srinu says:

    this is really awesome….

    your are making people rock……

Leave a Reply

Your email address will not be published. Required fields are marked *