Create an RSS Feed with PHP and MySQL

Create an RSS Feed with PHP and MySQL

RSS feed is best solution for getting backlinks to your website and for increasing your website traffic. In this article you'll learn how to create your own custom RSS feeds using PHP and MySQL.

We'll first learn how to create two database tables and then how to retrieve data from them which will be formatted into an RSS feed. Here's an example of the completed RSS feed and a download link to the code.

Basic structure of RSS feed



	
	Website Title 
	Website URL
	Website description
	en-us
	
		Item/Article Title
		Item/Article URL 
		Items/Article description 
	
	

Create table in database

CREATE TABLE IF NOT EXISTS `post` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `link` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
--
-- Dumping data for table `post`
--
INSERT INTO `post` (`id`, `title`, `link`, `description`) VALUES
(1, 'How to Build countdown timer in JavaScript', 'http://www.etutorialz.com/build-countdown-timer-in-javascript', 'Building countdown timer in javascript is quite easy. We just have to focus on core functions of javascript i.e. setInterval, Math, Date etc.'),
(2, 'Create Web Service in Java', 'http://www.etutorialz.com/create-webservice-in-java', 'Web Service is a method that is used for the purpose of exchanging the data with other web application via the web or cloud using HTTP,XML, SAOP etc. A main advantage of this is that it allows for communicate data between different languages.'),
(3, 'Why Java is a secure way to go for Software development', 'http://www.etutorialz.com/why-java-secure-way-to-go-for-software-development', 'Security is an important feature and Java’s security model is one of the key architectural features that make it most trustful choice when it comes to developing enterprise-level applications. Security becomes critical when software is downloaded across network and executed locally, and Java easily reduce the security vulnerabilities associated with the projects or applications. '),
(4, 'Wordpress Plugins to create social networking website', 'http://www.etutorialz.com/wordpress-plugins-to-create-social-networking-website', 'Social networking is a powerful tool for connecting peoples to each other. This is not easy to create social networking website like facebook and twitter but now you can create your social networking website in wordpress with functionalities like chatting, private messages, profile photo, status, Video uploading, images uploading, like, follow and more. '),
(5, 'How to generate dynamic pdf file in java application', 'http://www.etutorialz.com/how-to-generate-dynamic-pdf-file-in-java-application', 'We can create the pdf file using iText library. The iText library is supports the generation of HTML, PDF, RTF, and XML documents. It contains classes to generate the PDF file in different font, add water marks and to generate table in the PDF file ');

Files - index.php




    
    How to Create an RSS Feed with PHP and MySQL
    


    

How to Create an RSS Feed with PHP and MySQL

Files - rss.php

 PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
 
$pdo = connect();
 
// posts *******************************
$sql = 'SELECT * FROM post ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute();
$rs_post = $query->fetchAll();
 
// The XML structure
$data = '';
$data .= '';
$data .= '';
$data .= 'Etutorialz : technical hub for all';
$data .= 'http://www.etutorialz.com';
$data .= 'Free Web tutorials with source code, PHP Tutorials, JavaScript Tutorials, HTML Tutorials and CSS Tutorials';
foreach ($rs_post as $row) {
    $data .= '';
    $data .= ''.$row['title'].'';
    $data .= ''.$row['link'].'';
    $data .= ''.$row['description'].'';
    $data .= '';
}
$data .= '';
$data .= ' ';
header('Content-Type: application/xml');
echo $data;
?>

Style.css

* {
	margin: 0;
	padding: 0;
}
body {
	padding: 10px;
	background: #eaeaea;
	text-align: center;
	font-family: arial;
	font-size: 12px;
	color: #333333;
}
.container {
	width: 1000px;
	height: auto;
	background: #ffffff;
	border: 1px solid #cccccc;
	border-radius: 10px;
	margin: auto;
	text-align: left;
}
.header {
	padding: 10px;
}
.main_title {
	background: #cccccc;
	color: #ffffff;
	padding: 10px;
	font-size: 20px;
	line-height: 20px;
}
.content {
	padding: 10px;
	min-height: 100px;
	text-align: center;
}
.footer {
	padding: 10px;
	text-align: right;
}
.footer a {
	color: #999999;
	text-decoration: none;
}
.footer a:hover {
	text-decoration: underline;
}
.button_rss {
	width: 200px;
	height: 30px;
	display: block;
	text-align: center;
	background: #a3a3a3;
	border: 1px solid #808080;
	color: #ffffff;
	border-radius: 10px;
	cursor: pointer;
	margin: auto;
	margin-top: 20px;
	font-size: 14px;
	line-height: 30px;
	font-weight: bold;
	text-decoration: none;
}

As above explained steps, you can easily create RSS by using PHP and Mysql. Here we have also shared zip file of code of this article. You can download this code and implement with your website.

Download Code