29 lines
964 B
PHP
29 lines
964 B
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
try {
|
|
// 1. Connect without a database selected
|
|
$pdo_admin = new PDO('mysql:host='.DB_HOST.';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
]);
|
|
|
|
// 2. Create the database if it doesn't exist
|
|
$pdo_admin->exec("CREATE DATABASE IF NOT EXISTS ".DB_NAME);
|
|
echo "Database '" . DB_NAME . "' created or already exists.\n";
|
|
|
|
// 3. Now, connect to the specific database and create the table
|
|
$pdo = db();
|
|
$sql = "
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
display_name VARCHAR(50) NOT NULL,
|
|
email VARCHAR(100) NOT NULL UNIQUE,
|
|
password_hash VARCHAR(255) NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);";
|
|
$pdo->exec($sql);
|
|
echo "Table 'users' created successfully (if it didn't exist).\n";
|
|
|
|
} catch (PDOException $e) {
|
|
die("DB SETUP ERROR: " . $e->getMessage());
|
|
} |