NameSilo

RSS Feed

Spaceship Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hi. I was wondering whether anybody could help me. I'm just looking for a simple script which can retrieve the last 10 results from a MySQL table and then write this as an XML file (for RSS feeds). I have read many tutorials but they all seem to over-complicate things.

Willing to donate NP$. Thanks,

miseria

UPDATE: I've found this tutorial which is brilliant. However, my XML has the extension .php. Is this a problem? (http://www.sqldumpster.com/xml.php)
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
This is a script that I wrote to perform the same function for my website. If you have any questions feel free to ask. Enjoy!

Note: You will need to change a few things in order for it to work on your website. (i.e. database, username, password, and tablename.)

The database table that I used was setup with the fields id, title, description, and link so you may need to make a few adjustments to fit your table.



Code:
<?php

// Establishes db connection
$database = "database";
$username = "username";
$password = "password";
mysql_connect("localhost", $username, $password) || die ("Unable to select database");
mysql_select_db($database);

//Path and name to save xml file 
$output = "feeds/feed.xml";

//Opens output file for writing
$open = fopen($output, "w+");

//Writes standard tags output file
fwrite($open, "<?xml version=\"1.0\" ?>\n");
fwrite($open, "<rss version=\"2.0\">\n");
fwrite($open, "<channel>\n");
fwrite($open, "<title>Enter site title here</title>\n");
fwrite($open, "<description>Enter description of your site there</description>\n");
fwrite($open, "<link>http://www.yoursite.com</link>\n");

// Grabs last 10 records
$query = "SELECT * FROM tablename ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
$num = mysql_num_rows($result); 
$i = 0;

while ($i < $num) {

 $title = mysql_result($result,$i,"title");
 $desc = mysql_result($result,$i,"description");
 $link = mysql_result($result,$i,"link");

 fwrite($open, "<item>\n");
 fwrite($open, "<title>" . $title . "</title>\n");
 fwrite($open, "<description>" . $desc . "</description>\n");
 fwrite($open, "<link>" . $link . "</link>\n");
 fwrite($open, "</item>\n");

$i++;
}

// Writes standard closing tags
fwrite($open, "</channel>\n");
fwrite($open, "</rss>");

//Closes output file
$close = fclose($open);

echo "RSS feed generated!";

?>

LOL! I didn't see the update that you found a tutorial! I would recommend renaming it to .xml just to follow the standard. I am not sure but I would think with the wrong extension it may cause problems with some feed readers.
 
0
•••
Hi netzilla. Thanks for the script. I have donated NP$ for your help
 
0
•••
Received. Thank you very much!
 
0
•••
cool!
but what would you cahnge the "tablename" to?
 
0
•••
whatever the table name is that is holding the data that you want to put in the rss feed
 
0
•••
Interesting script.

I'll give it a try as soon as I find some time.

Thanks for sharing.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back