Use PHP to call MySql data to generate Sitemap site map

Original link: https://www.zyzhang.com/%E4%BD%BF%E7%94%A8php%E8%B0%83%E5%8F%96mysql%E6%95%B0%E6%8D%AE% E7%94%9F%E6%88%90sitemap%E7%BD%91%E7%AB%99%E5%9C%B0%E5%9B%BE/

I always wanted to make a Sitemap sitemap myself, but I couldn’t. I checked a lot of strategies and couldn’t use it. Until recently, I finally found an available one (technical problems still have to rely on Google), translated and recorded as follows.

Create a new file, for example named sitemap.php, modify it according to your own needs and enter the following code, and then save it. Directly open https://ift.tt/vu8Llfq to use, although it is not in xml format, but Google and Baidu can support it.

 <?php //sitemap.php $connect = mysqli_connect("localhost", "root", "!AAA", "!BBB"); $query = "SELECT ccontent FROM !CCC order by !DDD desc limit !EEE"; $result = mysqli_query($connect, $query); $base_url = "http://www.!FFF.com/"; header("Content-Type: application/xml; charset=utf-8"); echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL; while($row = mysqli_fetch_array($result)) { echo '<url>' . PHP_EOL; echo '<loc>'.$base_url. $row["ccontent"] .'</loc>' . PHP_EOL; echo '<changefreq>daily</changefreq>' . PHP_EOL; echo '</url>' . PHP_EOL; } echo '</urlset>' . PHP_EOL; ?>

!AAA, change to your database password

!BBB, change to the data table to use.

!CCC, read data column instead.

!DDD, change to the sorting method, here is the reverse order, if you need the positive order, delete the following desc.

!EEE, change to the number of data bars to be called.

!FFF, change to the prefix of the URL, for example, the prefix is ​​https://ift.tt/kG5rdoh, the data in the data table is ABC, then the link in the Sitemap is “https://ift.tt/i83x6Jv”

Using PHP to retrieve MySql data to generate a Sitemap sitemap first appeared in Laughing Books .

This article is transferred from: https://www.zyzhang.com/%E4%BD%BF%E7%94%A8php%E8%B0%83%E5%8F%96mysql%E6%95%B0%E6%8D%AE% E7%94%9F%E6%88%90sitemap%E7%BD%91%E7%AB%99%E5%9C%B0%E5%9B%BE/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment