The other day we created a xml sitemap using information retrieved from a database with php, we thought we’d show you the code on how to do it:
-
<?php
-
$conn = mysql_connect("dblocation", "username", "password") or die('Error connecting to mysql');
-
mysql_select_db(dbname);
-
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
-
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
-
$sql = "select * from tablename GROUP BY fieldname";
-
$rs = mysql_query($sql, $conn);
-
while($row=mysql_fetch_array($rs)) {
-
echo "<url>\n";
-
echo "<loc>http://www.your-url.com/".$row['abbr'].".html</loc>\n";
-
echo "<lastmod>2008-09-29</lastmod>\n";
-
echo "<changefreq>monthly</changefreq>\n";
-
echo "
-
<priority>0.5</priority>\n";
-
echo "</url>\n";
-
}
-
echo "</urlset>" ;
-
mysql_close($conn);
-
?>
Save as sitemap.php and submit to search engines. Also in your robots.txt add: sitemap: http://www.your-url.com/sitemap.php
This code will return this xml source output for EACH record in your database:
<url>
<loc>http://www.your-url.com/page-path.html</loc>
<lastmod>2008-09-29</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
Posted under Web Design
This post was written by Coyol on September 30, 2008
