Gabriele
Gabriele

Reputation: 3

RSS Feed won't work: something wrong with <?xml version="1.0" encoding="iso-8859-1"?>

I've been writing a simple PHP script which is supposed to generate a RSS Feed extracting data from a MySQL database.

<?php
require("$_SERVER[DOCUMENT_ROOT]mysql.php");
$type = $_GET["type"];
$result = mysql_query("SELECT * FROM Setting WHERE Type = \"$type\"");
header("Content-Type: text/xml");
echo "
<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
<rss version=\"2.0\">
  <channel>
    <title>Vhannibal &ndash; I migliori setting per Dreambox! &ndash; Feed RSS</title>
    <link>http://www.vhannibal.net/</link>
    <description>".$type."</description>
";
while ($row = mysql_fetch_array($result))
{
    extract($row);
    echo "
    <item>
      <title>$row[Name]</title>
      <link>http://www.vhannibal.net/download_setting.php?id=$row[ID]</link>
      <description>".strftime("%e %b", $row["Date"])."</description>
    </item>
  </channel>
</rss>
";
}
?>

The problem is <?xml version="1.0" encoding="iso-8859-1"?> comes after an unwanted empty line generated by the PHP script which I think is the reason why it doesn't work. Am I right? How can I solve it? Thanks.

Upvotes: 0

Views: 2083

Answers (1)

guido
guido

Reputation: 19194

Your problem is the &ndash; entity in the title element which is not valid in xml (it's an html defined entity); just use "-" or use the decimal version: &#8210;.

Also, you need to put the channel and rss end tags outside of the while cycle.

To get rid of the first blank line, just replace

   echo "
    <?xml version="1.0" encoding="iso-8859-1"?>
    [...]

with

   echo "<?xml version="1.0" encoding="iso-8859-1"?>
   [...]

Upvotes: 2

Related Questions