我有這段代碼,我正在嘗試從中創建站點地圖,我需要一些幫助。當我運行 php 檔案時,我在螢屏上得到檔案的輸出,但沒有創建 sitemap.xml 檔案,有人知道為什么嗎?
<?
$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 = 'user';
$passdb = 'ps';
$namedb = 'db';
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 id, shortUrl FROM shorturl WHERE id BETWEEN 15 AND 45000";
$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 .= '
<br><br>
<url><br>
<loc>https://website.com/'. $row['shortUrl'] .'<loc><br>
<changefreq>monthly<changefreq>
<priority>1<priority><br>
<url>
';
}
}
uj5u.com熱心網友回復:
您必須將$xmlfile變數的內容寫入檔案。
file_put_contents('sitemap.xml', $xmlfile);在foreach回圈之后嘗試。
但我想知道是否<br>適用于 xml
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/404882.html
標籤:
上一篇:如何在VSCodeforPHP8.1EnumClass中更正語法突出顯示?
下一篇:獲取JSON的特定部分
