RSS feed using PHP/MySQL

Demonstrates how to create an RSS feed using PHP/MySQL

// the query to get your different items for the RSS feed
$query = "select link, title, description from `news` limit 15";

$dbconnect = mysql_connect('localhost', 'dbuser', 'dbpassword');
mysql_select_db('dbname', $dbconnect);

// we will use $result on line 30
$result = mysql_query($query, $dbconnect);

@header("Content-Type: application/rss+xml");
echo '<?xml version="1.0" encoding="utf-8"?>';
<rss version="2.0">
    <title>Title of your Feed</title>
    <description>This is just a feed for my blah blah blah.</description>
    <pubDate><?php echo date("D, d M Y H:i:s T"); ?></pubDate>
    <lastBuildDate><?php echo date("D, d M Y H:i:s T"); ?></lastBuildDate>
    <generator>PHPSnaps Simple RSS Feed</generator>
while ($row = mysql_fetch_assoc($result))
	echo "<item>";
	echo "		<title>" . htmlentities($row['title']) . "</title>";
	echo "		<link>" . htmlentities($row['link']) . "</link>";
	echo "		<description>" . htmlentities($row['description']) . "</description>";
	echo "</item>";




You will need to edit this to fit your needs, but it shows how simple it is to create an RSS feed using PHP and MySQL. Change the query to go along with your news/blog table, and edit the XML parts to accurately go with your webpage. If you alter the query structure be sure to check the while loop that starts on line 30. < link > should point to the webpage that particular news entry can be located.


Posted on 19.03.2011 20:48

it doesnt work u wanker

Add your comment