Dynamically generate your website's RSS with PHP and MySQL

by Koree S. Monteloyola


This is the script I use to dynamically generate my site's RSS using php and mysql.

$sql = "SELECT * FROM mydatabase.mysite_articles";
$result = mysql_query($sqll) or die("Cannot generate rss");
$numrows = mysql_num_rows($result);
if($numrows > 0){
    $now = date("D, d M Y H:i:s T");
    $string = "<?xml version="1.0"?>
            <rss version="2.0">
                <title>title of your site here</title>
                <description>".htmlentities(site's description here)."</description>
                <docs>url as reference of rss documentation</docs>
                <webMaster>emailhere@yoursite.com</webMaster> ";
    while($row = mysql_fetch_array($result)){
        $string .= "<item> ";
        $string .= "<title>".$row[title']."</title> ";
        $string .= "<link>".$row['link']."</link> ";
        $string .= "<description>".$row['link']."</description> ";
        $output .= "<pubDate>".date("D, d M Y H:i:s T",$row['date_timestamp']))."</pubDate> ";
        $string .= "</item> ";
    $string .= "</channel> ";
    $string .= "</rss> ";
    header("Content-Type: application/rss+xml");
    echo $string;

Using a content-disposition header,  which specifies the output and presentation of the body part as an xml/rss, there is no need to create a separate rss file (through fopen/fwrite).

The content-disposition header is being specified through this line of code:

header("Content-Type: application/rss+xml");


Category: MySQL, PHP, Tutorials, Web
blog comments powered by Disqus