<?php
header("Content-Type: application/xml; charset=utf-8");
error_reporting(0);

// ========== 数据库配置（已填好你的信息） ==========
$db_host = 'localhost';
$db_user = 'wfd1028';
$db_pwd  = 'wfd19871028';
$db_name = 'wfd1028';
$table   = 'tb_news'; // 文章表是 tb_news

// 连接数据库
$conn = mysqli_connect($db_host, $db_user, $db_pwd, $db_name);
mysqli_set_charset($conn, 'utf8mb4');

if (!$conn) {
    exit('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><error>数据库连接失败</error></urlset>');
}

// 查询：tb_news 按id倒序
$sql = "SELECT id, title, addtime FROM `$table` ORDER BY id DESC";
$result = mysqli_query($conn, $sql);

if(!$result){
    $errmsg = mysqli_error($conn);
    exit('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><error>SQL错误：'.htmlspecialchars($errmsg).'</error></urlset>');
}

echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

while ($row = mysqli_fetch_assoc($result)) {
    $id       = intval($row['id']);
    $title    = htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
    $lastmod  = date('Y-m-d', $row['addtime']);
    $loc      = htmlspecialchars("http://www.ysyn.com.cn/article/{$id}.html", ENT_QUOTES, 'UTF-8');
?>
<url>
<loc><?php echo $loc; ?></loc>
<title><?php echo $title; ?></title>
<lastmod><?php echo $lastmod; ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php
}

echo '</urlset>';
mysqli_close($conn);
?>
