I have been racking my brain trying to think of how this would work to no success. Can anyone explain how this could be done?

Dani AI

Generated

Short answer for : yes — pull the rows with MySQLi and emit a well-formed RSS document. was right that the feed is XML, and 's example shows the basic layout, but real-world feeds should handle encoding, dates, HTML in descriptions, and safe DB access.

Practical checklist before you output anything: set the MySQLi charset to utf8mb4, use prepared statements if the query takes input, return only the most recent N published posts, use absolute URLs, and format dates with PHP's DATE_RSS (RFC‑2822). Put HTML post bodies inside CDATA so aggregators don't choke, include a stable <guid> for each item, and send the MIME type application/rss+xml; charset=utf-8. Also add simple HTTP caching (Cache-Control / Last-Modified or ETag) so clients and proxies don't re-request constantly.

A robust pattern is to build the feed with DOMDocument (or SimpleXML) instead of echoing raw strings — this avoids escaping bugs and makes CDATA easy. Example (MySQLi + DOMDocument):

<?php
$mysqli = new mysqli('host','user','pass','db');
if ($mysqli->connect_errno) { http_response_code(500); exit; }
$mysqli->set_charset('utf8mb4');

$stmt = $mysqli->prepare(
  "SELECT id,title,slug,content,created_at
   FROM posts WHERE status='published'
   ORDER BY created_at DESC LIMIT 20"
);
$stmt->execute();
$res = $stmt->get_result();

$dom = new DOMDocument('1.0','utf-8');
$rss = $dom->createElement('rss');
$rss->setAttribute('version','2.0');
$dom->appendChild($rss);

$ch = $dom->createElement('channel');
$ch->appendChild($dom->createElement('title','Example Blog'));
$ch->appendChild($dom->createElement('link','https://example.com'));
$ch->appendChild($dom->createElement('description','Latest posts'));
$rss->appendChild($ch);

while ($row = $res->fetch_assoc()) {
  $item = $dom->createElement('item');
  $item->appendChild($dom->createElement('title',$row['title']));
  $item->appendChild($dom->createElement('link','https://example.com/post/'.$row['slug']));
  $item->appendChild($dom->createElement('guid','https://example.com/post/'.$row['slug']));
  $item->appendChild($dom->createElement('pubDate', date(DATE_RSS, strtotime($row['created_at']))));
  $desc = $dom->createElement('description');
  $desc->appendChild($dom->createCDATASection($row['content']));
  $item->appendChild($desc);
  $ch->appendChild($item);
}

header('Content-Type: application/rss+xml; charset=utf-8');
echo $dom->saveXML();

Troubleshooting notes: if get_result() isn't available, bind_result()/fetch() is the fallback. Run the final feed through a validator and test in a few readers. Watch timezone handling for pubDate, and never expose drafts or private posts.

Recommended Answers

All 2 Replies

An RSS feed is nothing more than an XML file. What exactly is the problem you're having? You can retrieve the data as usual, and then create a feed by applying a template.

Here is the basic structure of an RSS feed.

<?xml version='1.0' encoding='UTF-8'?> 
<rss version='2.0'> 
<channel> 

  <title>Website related title or RSS Feed Title </title> 
  <link>URL to your site</link> 
  <description>Description for your Site/Feed</description> 

  <item> 
    <title>Item Title</title> 
    <link>Item URL </link> 
    <description>Items description </description> 
  </item> 

</channel> 
</rss>

Then, create a php page...something like this...

<?php

// your db related code...

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

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

while( // $row data from your db query)
{
$title=$row['title']; 
$link=$row['link']; 
$description=$row['description']; 

echo "<item> 
<title>$title</title>
<link>$link</link>
<description>$description</description>
</item>"; 
} 
echo "</channel></rss>"; 
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.