使用的是 PHP + MySQL 构建的网站,以下是一个简单的示例

黄文博客 / 2024-10-12 / 原文

// index.php
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 查询网站名称
$sql = "SELECT name FROM website_settings WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        $websiteName = $row["name"];
    }
} else {
    echo "0 results";
}
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($websiteName); ?></title>
</head>
<body>
    <h1>Welcome to <?php echo htmlspecialchars($websiteName); ?></h1>
    <!-- 其他内容 -->
</body>
</html>