75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
$host = 'localhost';
|
|
$user = 'root';
|
|
$pass = ''; // Default XAMPP password
|
|
$charset = 'utf8mb4';
|
|
|
|
// 1. Connect to MySQL server (without DB) to create database
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;charset=$charset", $user, $pass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Create Database if not exists
|
|
$pdo->exec("CREATE DATABASE IF NOT EXISTS sudoku CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
|
echo "Database 'sudoku' successfully checked/created.<br>";
|
|
} catch (PDOException $e) {
|
|
die("DB Connection failed: " . $e->getMessage());
|
|
}
|
|
|
|
// 2. Connect to the specific Database 'sudoku'
|
|
try {
|
|
$dsn = "mysql:host=$host;dbname=sudoku;charset=$charset";
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
die("Connection to 'sudoku' failed: " . $e->getMessage());
|
|
}
|
|
|
|
// 3. Create 'users' table
|
|
try {
|
|
$sqlUsers = "
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50) NOT NULL UNIQUE,
|
|
password VARCHAR(255) NOT NULL
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
|
|
|
$conn->exec($sqlUsers);
|
|
echo "Table 'users' successfully checked/created.<br>";
|
|
|
|
// Seed default users if table is empty
|
|
$check = $conn->query("SELECT count(*) FROM users")->fetchColumn();
|
|
if ($check == 0) {
|
|
$password = password_hash('123456', PASSWORD_DEFAULT); // Default password
|
|
$sqlInsert = "INSERT INTO users (username, password) VALUES
|
|
('admin', '$password'),
|
|
('player1', '$password')";
|
|
$conn->exec($sqlInsert);
|
|
echo "Default users (admin, player1) created with password '123456'.<br>";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
echo "Error Creating 'users': " . $e->getMessage() . "<br>";
|
|
}
|
|
|
|
// 4. Create 'leaderboard_sudoku' table
|
|
try {
|
|
$sqlLeaderboard = "
|
|
CREATE TABLE IF NOT EXISTS leaderboard_sudoku (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50),
|
|
difficulty VARCHAR(10),
|
|
time_seconds INT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)";
|
|
|
|
$conn->exec($sqlLeaderboard);
|
|
echo "Table 'leaderboard_sudoku' successfully checked/created.<br>";
|
|
|
|
} catch (PDOException $e) {
|
|
echo "Error Creating 'leaderboard_sudoku': " . $e->getMessage() . "<br>";
|
|
}
|
|
|
|
echo "<hr><strong>Setup Complete!</strong> You can now use the application.";
|
|
?>
|