How to Create an RSS Feed with PHP and MySQL

How to Create an RSS Feed with PHP and MySQL

Want to increase your website traffic, and get more backlinks to your website, so the RSS feed would be one of the best solution. In this tutorial I’ll show you how to create an rss feed with PHP and MySQL.

Download source code

This example displays a web page with single button, when we click on this button, an XML file will be displayed in the RSS format. But the content of this file will be loaded dynamically from the database.

1. RSS sample

This is an example for the rss output in xml format.

<rss version="2.0">
    <channel>
        <title>Bewebdeveloper : Free web tutorials</title>
        <link>http://www.bewebdeveloper.com</link>
        <description>Free Web tutorials with source code, PHP Tutorials, JavaScript Tutorials, HTML Tutorials and CSS Tutorials</description>
        <item>
            <title>Dynamic Drag and Drop with jQuery and PHP</title>
            <link>http://www.bewebdeveloper.com/tutorial-about-dynamic-drag-and-drop-with-jquery-and-php</link>
            <description>A new tutorial about Dynamic Drag and Drop With jQuery and PHP</description>
        </item>
        <item>
            <title>Crop photo using PHP and jQuery</title>
            <link>http://www.bewebdeveloper.com/tutorial-about-crop-photo-using-php-and-jquery</link>
            <description>A new tutorial about Crop photo using PHP and jQuery</description>
        </item>
    </channel>
</rss>

1. Database

Create a new database named "rss_feed", and import the SQL script in main folder, or copy/paste the script bellow:

--
-- Table structure for table `post`
--

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, 'Installing and configuring a web environment for PHP/MySQL', 'http://www.bewebdeveloper.com/tutorial-about-installing-and-configuring-a-web-environment-for-php-mysql', 'Hi, I am Ali Aboussebaba, Web and Mobile Engineer. Having worked with various technologies ranging from web to mobile. In this first tutorial, i’ll show you how to install and configure a web environment using WampSerer.'),
(2, 'Using Ajax with native JavaScript', 'http://www.bewebdeveloper.com/tutorial-about-using-ajax-with-native-javascript', 'In this tutorial, I’ll show you how to execute PHP scripts in the web server using Ajax with native JavaScript.'),
(3, 'Using Ajax with PHP/MySQL', 'http://www.bewebdeveloper.com/tutorial-about-using-ajax-with-phpmysql', 'Hi guys, in this tutorial, w’ll use Ajax with PHP/MySQL. A small administration page, with listing, adding and deleting members using Ajax with PHP/MySQL.'),
(4, 'Autocomplete using PHP/MySQL and jQuery', 'http://www.bewebdeveloper.com/tutorial-about-autocomplete-using-php-mysql-and-jquery', 'Hi, a new tutorial explain how to impliment an autocomplete using PHP/MySQL and jQuery is available. In this tutorial w’ll focus on the autocomplete functionality using a single table in the database and a single input text in the web page. When we type a letter, a list of proposition containning the keyword will be displayed with the keyword in bold.'),
(5, 'Crop photo using PHP and jQuery', 'http://www.bewebdeveloper.com/tutorial-about-crop-photo-using-php-and-jquery', 'Give the possibility for users to upload photos dynamically is good, but users upload photos with different sizes, and usually that cause problem in the website design. So in this tutorial I’ll show you how to crop photo using PHP and jQuery. Don’t worry, it’s simple, just follow the tutorial step by step.'),
(6, 'Dynamic Drag and Drop with jQuery and PHP', 'http://www.bewebdeveloper.com/tutorial-about-dynamic-drag-and-drop-with-jquery-and-php', 'A new tutorial about Dynamic Drag and Drop With jQuery and PHP is available. Need to order your items quickly and easily ? so read this tutorial step by step. A basic knowledge about PHP and jQuery is required.');

3. Files

index.php

This is the main file, it will be displayed on the first view

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to Create an RSS Feed with PHP and MySQL</title>
    <link rel="stylesheet" href="css/style.css" />
</head>

<body>
    <div class="container">
        <div class="header">
            <img src="images/BeWebDeveloper.png" />
        </div><!-- header -->
        <h1 class="main_title">How to Create an RSS Feed with PHP and MySQL</h1>
        <div class="content">
            <a href="rss.php" class="button_rss">Click to get RSS feed</a>
        </div><!-- content -->    
        <div class="footer">
            Powered by <a href="http://www.bewebdeveloper.com">bewebdeveloper.com</a>
        </div><!-- footer -->
    </div><!-- container -->
</body>
</html>

rss.php

The rss feed will be generated here. W’ll load the list of posts from the database using PHP PDO, and display this content as an xml file using the php header, header('Content-Type: application/xml');

<?php
// PDO connect *********
function connect() {
    return new PDO('mysql:host=localhost;dbname=rss_feed', 'root', '', array(PDO::ATTR_ERRMODE => 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 = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>Bewebdeveloper : Free web tutorials</title>';
$data .= '<link>http://www.bewebdeveloper.com</link>';
$data .= '<description>Free Web tutorials with source code, PHP Tutorials, JavaScript Tutorials, HTML Tutorials and CSS Tutorials</description>';
foreach ($rs_post as $row) {
    $data .= '<item>';
    $data .= '<title>'.$row['title'].'</title>';
    $data .= '<link>'.$row['link'].'</link>';
    $data .= '<description>'.$row['description'].'</description>';
    $data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';

header('Content-Type: application/xml');
echo $data;
?>

style.css

This is the CSS file, it contain the design styles

* {
	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;
}
PHP, RSS Feed
comments powered by Disqus

Social Profiles

bewebdeveloper on facebook bewebdeveloper on twitter bewebdeveloper on google plus bewebdeveloper on vk bewebdeveloper on youtube bewebdeveloper on tumblr bewebdeveloper rss

Subscribe to our Newsletter

Facebook

Twitter

Google+

HTML Tutorials
CSS Tutorials

Advertising