RSS / Atom Feed Reader Online

How to create an RSS feed from data stored in a MySQL database

Step 1: Create a MySQL Database and Table

First, create a table in your MySQL database where you will store articles or posts that will be included in the RSS feed.


CREATE TABLE articles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    link VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    pubDate DATETIME NOT NULL
);

You can insert some dummy data into the `articles` table:


INSERT INTO articles (title, link, description, pubDate) VALUES
('First Article', 'https://example.com/articles/1', 'This is the first article.', NOW()),
('Second Article', 'https://example.com/articles/2', 'This is the second article.', NOW());

Step 2: Create the PHP Script to Generate the RSS Feed

Now, create a PHP file (e.g., `rss.php`) to generate the RSS feed.


<?php
// Database connection details
$host = 'localhost';
$dbname = 'your_database_name';
$username = 'your_username';
$password = 'your_password';
// Connect to the database
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit();
}
// Fetch articles from the database
$sql = "SELECT title, link, description, pubDate FROM articles ORDER BY pubDate DESC LIMIT 10";
$stmt = $pdo->query($sql);
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Set the content type to XML
header('Content-Type: application/rss+xml; charset=utf-8');
// RSS XML structure
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<rss version='2.0'>\n";
echo "<channel>\n";
echo "<title>Your Site's RSS Feed</title>\n";
echo "<link>https://example.com</link>\n";
echo "<description>This is the RSS feed for Your Site</description>\n";
echo "<language>en-us</language>\n";
echo "<pubDate>" . date(DATE_RSS) . "</pubDate>\n";
foreach ($articles as $article) {
    echo "<item>\n";
    echo "<title>" . htmlspecialchars($article['title']) . "</title>\n";
    echo "<link>" . htmlspecialchars($article['link']) . "</link>\n";
    echo "<description>" . htmlspecialchars($article['description']) . "</description>\n";
    echo "<pubDate>" . date(DATE_RSS, strtotime($article['pubDate'])) . "</pubDate>\n";
    echo "</item>\n";
}
echo "</channel>\n";
echo "</rss>\n";
?>

Explanation of the Code:

  • Database Connection: The script connects to the MySQL database using PDO.
  • Querying Articles: It fetches the latest 10 articles from the `articles` table and stores them in the `$articles` array.
  • RSS Header: It sets the content type to `application/rss+xml` to ensure that the browser treats it as an RSS feed.
  • RSS Structure: The RSS feed is built using XML tags, following the standard RSS 2.0 structure. Each article is added as an `<item>` with a `<title>`, `<link>`, `<description>`, and `<pubDate>`.
  • Special Characters: The `htmlspecialchars` function is used to ensure that special characters like `&` and `<` are properly escaped in XML.

Step 3: Access the RSS Feed

Save the PHP file (e.g., `rss.php`) on your server. You can now access your RSS feed by navigating to the URL where the script is hosted, for example:


https://yourdomain.com/rss.php

Step 4: Testing the RSS Feed

You can test your RSS feed in a web browser or by using an RSS reader to confirm that it is working correctly.

Optional: Add More RSS Tags

You can extend the RSS feed by adding additional tags like `<author>`, `<category>`, `<guid>`, etc., based on the requirements of your application.

Example of adding an `<author>` tag:


echo "<author>[email protected] (Author Name)</author>\n";

You now have a working RSS feed that pulls data from a MySQL database and generates an RSS XML output using PHP. You can further customize this script as needed, for example by adding categories, images, or other metadata to your feed.