Php-mysql Course

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
 From: https://coursesweb.net/
*/
$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="http://www.sitemaps.org/schemas/sitemap/0.9">';

// 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>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
    }
  }

  $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="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
 <loc>https://coursesweb.net/</loc>
 <priority>0.5</priority>
 <changefreq>weekly</changefreq>
</url>
<url>
 <loc>https://coursesweb.net/php-mysql/</loc>
 <priority>0.5</priority>
 <changefreq>weekly</changefreq>
</url>
.....
</urlset>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
XML sitemap with data from MySQL

Last accessed pages

  1. Check the file type before Upload (9278)
  2. jQuery Drag and Drop Rows between two similar Tables (12796)
  3. Read Excel file data in PHP - PhpExcelReader (96701)
  4. HTML Symbol Entities (1252)
  5. Insert, Select and Update NULL value in MySQL (59004)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (299)
  2. Read Excel file data in PHP - PhpExcelReader (102)
  3. The Four Agreements (90)
  4. PHP Unzipper - Extract Zip, Rar Archives (87)
  5. The Mastery of Love (83)
Chat
Chat or leave a message for the other users
Full screenInchide