Create RSS Feed with PHP Mysql

Abhigyan Singh 10th Oct 2020

RSS

create an RSS feed is playing the main role in syndicating information to large audiences. By this feed, you can update your audience with your latest content or information.

RSS means that if you have some registered audiences then you can share your new updates and news directly to your audience on their registered e-mails ID.

In this tutorial, I will explain how to populate your database content to RSS. Here in the RSS feed, we will display some useful content like title, description, date, and image, etc. For this, we will use PHP and MySQL to populate content for the feed.

Here we will retrieve this content from the database and format these content into RSS format.
Steps are given below: Create a table for getting data for RSS.

CREATE TABLE IF NOT EXISTS `table_rss` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`description` text NOT NULL,
`url` text NOT NULL,
`image` varchar(255) NOT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;

Create a php like:

<?php
define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", ""); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME",""); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

$query = "SELECT title, description, image, url FROM ".table_rss." WHERE status=1 ORDER BY created_date desc";
$getBlogDisplay = mysql_query($query);

header("Content-type: text/xml");

echo "<?xml version='1.0' encoding='UTF-8'?-->
<rss version="2.0">
<channel>
<title></title>
<link>
<description></description>
<language>en-us</language>";


while($dataBlogDisplay = mysql_fetch_array($getBlogDisplay)){

$title = $dataBlogDisplay['title'];
$link = $dataBlogDisplay['url'];
$description = $dataBlogDisplay['image'];

echo "<item>
<title>$title</title>
<link>https://discussdesk.com//$link.htm
<description>$description</description>
</item>";
}
echo "</channel></rss>";
?>

That's all about RSS Feed. Just download and start feeding your content.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk