Create Google Sitemap With PHP And Mysql

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:

  1. <?php
  2. $conn = mysql_connect("dblocation", "username", "password") or die('Error connecting to mysql');
  3. mysql_select_db(dbname);
  4. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  5. echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
  6. $sql = "select * from tablename GROUP BY fieldname";
  7. $rs = mysql_query($sql, $conn);
  8. while($row=mysql_fetch_array($rs))  {
  9.  echo "<url>\n";
  10.  echo "<loc>http://www.your-url.com/".$row['abbr'].".html</loc>\n";
  11.  echo "<lastmod>2008-09-29</lastmod>\n";
  12.  echo "<changefreq>monthly</changefreq>\n";
  13.  echo "
  14. <priority>0.5</priority>\n";
  15.  echo "</url>\n";
  16. }
  17. echo "</urlset>" ;
  18. mysql_close($conn);
  19. ?&gt;

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

Tags: , , ,

9 Comments so far

  1. David Domingues September 30, 2008 11:27 pm

    That means you store every page of your site into a table. Not every site are made like this.

  2. Coyol October 1, 2008 9:08 am

    You’re right David, every page is stored in DB. Every site is no made like this BUT there are some that are and hopefully this will help them.

  3. Palestine Students Forum October 2, 2008 8:08 am

    how we use it
    it give this error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxx/public_html/sitemap.php on line 8
    ??????????????

  4. Coyol October 2, 2008 8:14 am

    Post your code… there is obviously something wrong in your sql request.

  5. Palestine Students Forum October 2, 2008 9:38 am

    we use it in vbulletin forums
    we change the username password dblocation dbname and url for my site
    but it give this error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxx/public_html/sitemap.php on line 8

  6. Palestine Students Forum October 2, 2008 9:42 am

    <?php
    $conn = mysql_connect(”localhost”, “kuraiks_roh”, “055511″) or die(’Error connecting to mysql’);
    mysql_select_db(kuraiks_vb);
    echo “\n”;
    echo “\n”;
    $sql = “select * from table GROUP BY field”;
    $rs = mysql_query($sql, $conn);
    while($row=mysql_fetch_array($rs)) {

  7. Coyol October 2, 2008 9:49 am

    I’m not real sure if this would work with vBulletin. But to find out, you need to edit this line:

    $sql = “select * from table GROUP BY field”;

    You can probably just delete the ‘GROUP BY field’ part, but you need to change ‘table’ to the table you’re pulling info from. So just like this:

    $sql = “SELECT * FROM yourtablename”;

    Then look for the field that serves the url for each post and change this part:

    http://www.your-url.com/“.$row['abbr'].”.html

    with just:

    “.$row['fieldname'].”

    try this and see how it works.

  8. Palestine Students Forum October 2, 2008 9:11 pm

    We try to change it
    and we reply to you
    regards

  9. charobnjak October 18, 2008 10:06 am

    Thank you, works like a charm

Leave a Comment

Name (required)

Email (required)

Website

Comments

More Blog Post