Files
SunShineMusic/init_db.php
2025-09-24 14:15:23 +00:00

33 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// 数据库连接参数
$host = 'localhost';
$rootUser = 'a1sax1m9i';
$rootPass = 'a1sax1m9i';
$dbname = 'a1sax1m9i';
try {
// 先连接到MySQL服务器不指定数据库
$pdo = new PDO("mysql:host=$host;charset=utf8", $rootUser, $rootPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 创建数据库
$pdo->exec("CREATE DATABASE IF NOT EXISTS $dbname");
// 连接到新创建的数据库
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $rootUser, $rootPass);
// 创建推荐表
$sql = "CREATE TABLE IF NOT EXISTS recommendations (
id INT AUTO_INCREMENT PRIMARY KEY,
song_name VARCHAR(255) NOT NULL,
artist_name VARCHAR(255) NOT NULL,
reason TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
$pdo->exec($sql);
echo "数据库和表创建成功!请关闭此页面,开始使用网站。";
} catch(PDOException $e) {
die("初始化失败: " . $e->getMessage());
}
?>