Hi forum,

Trying to create an RSS feed for my blog (not using a blog host or blog software). Seems to me the best way for me to do it is to use PHP in the XML to draw the last X posts from my MySQL database. So first, I added a .htaccess file to the directory with the line AddType application/x-httpd-php .xml ...to make the php work inside the xml file.

My rss.xml looks like:

<?php header('Content-type: text/xml'); ?>
<rss version="2.0">

(connection goes here)
(recordset goes here)
	<description>I like the Internet</description>

	mysql_select_db($aCon, $acCon);
    $top5q="SELECT blogIndex,title,content,DATE_FORMAT(date,'%a, %d %b %Y %T') AS rfcdate,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9,tag10 FROM rhododendrites_blog ORDER BY date DESC LIMIT 10";
    $recentp = mysql_query($top5q,$acCon) or die(mysql_error());
		{ ?>
				echo htmlentities(strip_tags($row['title'])); ?>
				<?php echo $row['content']; ?>
			<link>http://www.x.com/blog/post.php?blogid=<?php echo $row['blogIndex']; ?></link>
			<pubDate><?php echo $row['rfcdate']; ?></pubDate>
		<?php } ?> 


I changed my website, of course, to x.com and the names of a couple vars. I'm new at this stuff and don't know what I should be paranoid about :) Sorry.

So not only does it not show any records (other than the initial channel info) when I surf to the URL of the feed directly, but when I try to validate it, I get this:

This feed does not validate.
line 89, column 42: pubDate must be an RFC-822 date-time:
<pubDate><?php echo $row; ?></pubDate>

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
Feeds should not be served with the "application/x-httpd-php" media type

line 84, column 3: title should not be blank

line 90, column 2: item should contain a guid element

line 93, column 0: Missing atom:link with rel="self"

Help? :/ Spent hours on this so far and can't figure it out.

8 Years
Discussion Span
Last Post by barbz

looks like it is not parsing the php. maybe it's best to separate the php and xml. personally i use smarty templates to build the rss.

my rss code looks like this:

  require_once 'smarty/Smarty.class.php';
  $tpl = new Smarty();
  require_once './inc/defines.inc.php';
  $tpl->force_compile = false;
  $tpl->caching = 1;
  $tpl->cache_lifetime = 14400;     
  if ($tpl->is_cached('rss.tpl')) {
    $content = $tpl->fetch('rss.tpl');
  else {
    $content = '';
    $db_link = @mysql_connect(HOST, USER, PASS);
    $query = "<omitted>";
    $result = @mysql_query($query);
    if ($result) {
      $rss = array ();
      while ($row = mysql_fetch_assoc($result)) {
        $dp = explode('-', $row['date']);
        $dt = mktime(12, 0, 0, $dp[1], $dp[2], $dp[0]);
        $row['date'] = date("D, d M Y H:i:s ", $dt) . '+0100';
        $row['article'] = substr($row['article'], 0, strpos($row['article'], '</p>'));
        $row['article'] = htmlentities(strip_tags($row['article']));
        $rss[] = $row;
      $pd = date("D, d M Y H:i:s ", time()) . '+0100'; 
      $tpl->assign('PUBDATE', $pd);
      $tpl->assign('YEAR', date('Y'));
      $tpl->assign('RSS', $rss);
      $content = $tpl->fetch('rss.tpl');
    if ($db_link)
  header("Content-Type: application/xml; charset=UTF-8");
  echo $content;

and my template like this:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
    <title>Example - News</title>
    <copyright>Copyright 2005-{$YEAR} Example</copyright>
    {foreach from=$RSS item=item}

Edited by pritaeas: Removed foundation name


you could use this.. its very simple..

<?php $db = new mysqli("localhost", "root", "", "rss"); ?>
<?php header('Content-type: text/xml'); ?>
<?php echo "<?";?>xml version="1.0" encoding="iso-8859-1"<?php echo "?>";?>
<rss version="2.0" xmlns:atom="http://www.geocities.com/jas_jo679">
	<title>Making a Dynamic RSS Feed</title>
	<description>Getting items from the database.</description>
	<copyright>Your Copyright Information</copyright>
	<atom:link href="http://barbz.0fees.net/" rel="self" type="application/rss+xml" />

	$query = "SELECT * FROM `article`";
	$results = $db->query($query);
	$number = $results->num_rows;

	for ($i = 1; $i <= $number; $i++) {
		$row = $results->fetch_assoc();
		$title = $row['title'];
		$description = $row['body'];
		$link = $row['link'];
			<title><?php echo $title; ?></title>
			<description><?php echo $description; ?></description>
			<link><?php echo $link; ?></link>


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.