View Full Version : RSS Xml output using php

11-14-2007, 12:13 AM
hi guys im trying to get a clean XML output using php and mysql, to make a dynamic rss feed , but i cant find whats the problem with my script, if any1 have any idea it will be very appreciated, here is the code.

$dbconnect = mysql_pconnect(localhost,$username,$password);
mysql_select_db($database, $dbconnect);
$query = "SELECT id, title, itemcomment, picturelink FROM jos_seyret_items WHERE featured='1' order by id desc limit 15";
$result = mysql_query($query, $dbconnect);

while ($line = mysql_fetch_assoc($result))
$return[] = $line;

$now = date("D, d M Y H:i:s T");

$output = "<?xml version=\"1.0\"?>
<rss version=\"2.0\">
<title>What's hot</title>
<description>Latest Video on webcastr.com</description>


foreach ($return as $line)
$output .= "<item><title>".htmlentities($line['title'])."</title>

$output .= "</channel></rss>";
header("Content-Type: application/rss+xml");
echo $output;

11-14-2007, 12:40 AM
what isn't working? You mention a problem, but don't say what it is, it's far less effort for someone to look for the cause of a particular problem than it is to look for one of the many possible issues that could happen when reading from a database and writing to XML (assuming you haven't screwed up something more trivial...)

11-14-2007, 01:19 AM
sorry about that, i was trying to validate it using feedvalidator.org and this came up.

This feed does not validate.
line 1, column 1: XML parsing error: <unknown>:1:1: xml declaration not at start of external entity

but the true is not sure whats the problem, this php code should give me a clean xml output from our database and the idea was after i get this output parse it using megpie liebrary or something simmilar. but looking at the print, i can find whats wrong with the xml output or the error declaration.

11-14-2007, 02:07 AM
Have you checked that the output is what you expect? Are there any php error messages being output in the content?

After you make sure that the output is what you expect, make sure that the .php file (or if this is a .xml file, is your server setup to parse it through the php language engine?) is saved as an ANSI/ASCII file and not UTF-8 or Unicode (which would be outputting extra language characters before the <?php tag.)

11-14-2007, 02:39 AM
Once you get to the point of actually validating the output, you will find that the & in the link need to be changed to &amp;

11-14-2007, 02:49 AM
gotcha, will get into this right now, thanks a lot for the hints :P