Jump to content

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");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.