Simple PHP script to create XML sitemap with data (URL addresses) saved in MySQL database.
Data are selected from MySQL database with PHP PDO, and saved into a file on server (sitemap.xml).
- Code:
 Simple PHP script to create XML sitemap with data (URL addresses) saved in MySQL database
$xmlfile = 'sitemap.xml';

// this variable will contain the XML sitemap that will be saved in $xmlfile
$xmlsitemap = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="">';

// Connection data (server_address, name, password, database_name)
$hostdb = 'localhost';
$userdb = 'username';
$passdb = 'password';
$namedb = 'database_name';

try {
  // Connect and create the PDO object
  $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
  $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

  // Define and perform the SQL SELECT query
  $sql = "SELECT `col_url` FROM `table_name` WHERE 1";
  $result = $conn->query($sql);

  // If the SQL query is succesfully performed ($result not false)
  if($result !== false) {
    // Parse the result set, and add the URL in the XML structure
    foreach($result as $row) {
      $xmlsitemap .= '<url>
<loc>'. $row['col_url'] .'</loc>

  $conn = null;        // Disconnect
catch(PDOException $e) {
  echo $e->getMessage();

$xmlsitemap .= '</urlset>';
file_put_contents($xmlfile, $xmlsitemap);          // saves the sitemap on server

// outputs the sitemap (delete this instruction if you not want to display the sitemap in browser)
echo $xmlsitemap;
- In this line of code:
$sql = "SELECT `col_url` FROM `table_name` WHERE 1";
Replace "col_url" with the name of the column in which the URL addresses are stored.
Replace "table_name" with the name of your MySQL table.
Also, replace "col_url" in this variable: $row['col_url'] too.

This PHP script creates a valid XML sitemap, with this structure:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="">

Daily Test with Code Example

Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1"></div></body>';
$dochtml = new DOMDocument();
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    //
XML sitemap with data from MySQL

Last accessed pages

  1. Spring Garden Cleanup (23)
  2. Create simple Website with PHP (11484)
  3. Emma Christmas Tree (33)
  4. Barbie Games (362)
  5. Mir And Ror (19)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (45791)
  2. Read Excel file data in PHP - PhpExcelReader (34450)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (34339)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (30919)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28164)