How to Create a Dynamic Sitemap with PHP and a Database
How to Create a Dynamic Sitemap with PHP and a Database

Creating a dynamic sitemap for your website is crucial for search engine optimization (SEO) and improving the discoverability of your content. In this tutorial, we'll walk through the process of generating a sitemap using PHP and a MySQL database. This guide assumes you have a basic understanding of PHP and MySQL.

Step 1: Set Up Your Database

First, ensure your database is properly set up with the necessary tables. For this example, we'll assume you have a table named pages with columns id, title, url, and last_modified.


CREATE TABLE `pages` (
  `title` VARCHAR(255) NOT NULL,
  `url` VARCHAR(255) NOT NULL,
  `last_modified` DATETIME NOT NULL


Step 2: Connect to Your Database

Create a PHP script to connect to your MySQL database. Save this file as db_connect.php.


$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

Replace localhost, username, password, and database_name with your actual database credentials.

Step 3: Fetch Data from the Database

Next, we'll write a script to fetch the data from the pages table. Save this file as fetch_pages.php.


include 'db_connect.php';

$sql = "SELECT url, last_modified FROM pages";
$result = $conn->query($sql);

$pages = array();

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $pages[] = $row;
} else {
    echo "0 results";


Step 4: Generate the Sitemap

Now we'll create the sitemap. Save this file as sitemap.php.


include 'fetch_pages.php';

header("Content-Type: application/xml; charset=utf-8");

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="">';

foreach ($pages as $page) {
    echo '<url>';
    echo '<loc>' . htmlspecialchars($page['url']) . '</loc>';
    echo '<lastmod>' . date('c', strtotime($page['last_modified'])) . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';

echo '</urlset>';


This script will generate an XML sitemap in the proper format for search engines to read. The date('c', strtotime($page['last_modified'])) function formats the last modified date in the correct ISO 8601 format.

Step 5: Access Your Sitemap

Once everything is set up, you can access your sitemap by navigating to sitemap.php in your browser. For example, if your website is hosted at, go to to see your dynamically generated sitemap.


By following these steps, you can create a dynamic sitemap using PHP and a MySQL database. This sitemap will help search engines crawl and index your website more effectively, ultimately improving your site's SEO.

Regularly updating your database with new or modified content will ensure your sitemap stays current, providing search engines with the latest information about your website.


Further Enhancements

Error Handling: Improve the scripts with better error handling for database connections and queries.

Pagination: If your website has a large number of pages, consider implementing pagination in your sitemap to adhere to the maximum URL limits.

SEO Optimization: Adjust the changefreq and priority values based on the importance and update frequency of your pages.


By maintaining an up-to-date and dynamic sitemap, you can significantly enhance your website’s visibility and performance in search engine rankings.

< > GitHub

To engage in discussions and post comments, kindly log in or register to create an account.