Generate Dynamic XML Sitemap Using PHP/MySQL
Hello, you know its gonna be cool if you have your own sitemap been generated automatically from database, this is also for those who update items to db.
How i wrote this code is that, assuming you have an item in the db which details are shown in a page, just generate the links and submit to search engine, take a look at the below code
<?php
//Lets define our database details
define('DBHOST','localhost');
define('DBUSER','root');
define('DBNAME','sitemap');
define('DBPWD','');
define('DOMAIN','http://2netlodge.com');
//Ooh, what are we doing without the database? Lets initiate the MySQL database connection
$connect = new MySQLi(DBHOST,DBUSER,DBPWD,DBNAME)or die(mysqli_error());
//Yea, we all know this is a php code and file, but lets tell the browser that its an XML content
header('Content-Type: text/xml');
//Adding some sitemap essentials
echo <?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
//our link listing starts here
echo '<url>';
//get data from database
$query = $connect->query("select * from items");
while($row=$query->fetch_array(MYSQL_ASSOC))
{
echo '<loc>
',DOMAIN,'?page=',$row['id'],
'
</loc>';
//you can add some date
}
echo '</url>';
echo '</urlset>';
?>
CHANGE THESE TO YOURS
define(‘DBHOST’,’localhost’);
define(‘DBUSER’,’root’);
define(‘DBNAME’,’sitemap’);
define(‘DBPWD’,”);
define(‘DOMAIN’,’http://2netlodge.com‘);
If you want to turn the extension to .xml, just add this to your .htaccess
RewriteRule ^(.*).xml $1.php
Hope it helps