Jump to content

[SOLVED] XML Parsing Error: junk after document ele.....


nathanmaxsonadil

Recommended Posts

I'm trying to create a dynamic rss feed

but now when I got to the feed it gives me this error.

XML Parsing Error: junk after document element
Location: http://www.myblog.com/feednews/
Line Number 2, Column 1:<b>Warning</b>:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>/myhome_dir/feednews/index.php</b> on line <b>19</b><br />
^

<?php
//Tell the browser that we're XML
header("Content-type: text/xml");
//Lets connect to MySQL
$host = 'localhost';
$username = 'my_user';
$password = 'my_pass';
$db_name = 'my_db';
@mysql_connect($host, $username, $password);
@mysql_select_db($db_name);
$query = @mysql_query("SELECT title, author, content, date, id FROM `your_posts` ORDER BY `id` DESC LIMIT 10");
//Begin the RSS
$rss = "<rss version=\"2.0\">
<channel>
<title>My Blog!</title>
<description>My blog</description>
<link>http://www.myblog.com/blog</link>";
//Form each one of our items
while($r = mysql_fetch_assoc($query))
{
$rss .= "<item>
<title>{$r['title']}</title>
<author>{$r['author']}</author>
<pubDate>{$r['date']}</pubDate>
<description>{$r['content']}</description>
<link>http://www.myblog.com/view.php?id={$r['id']}</link>
</item>";
}
//Close up our channel and RSS
$rss .= "</channel>
</rss>";
//Output the RSS and die
die ($rss);
?>

What's the matter with my code above?

 

Looks like the query is failing. You are suppressing errors, so it might be a good idea to add some error detection while still in production:

 

<?php
$query = @mysql_query("SELECT title, author, content, date, id FROM `your_posts` ORDER BY `id` DESC LIMIT 10") or die(mysql_error());
?>

Ok i got it to work now :) I had put `your_posts` instead of `news`

and I changed my mysql query from

mysql_connect("localhost", "username", "pass");

mysql_select_db("db");

to

mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("db");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.