-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.php
More file actions
40 lines (33 loc) · 1.4 KB
/
init_db.php
File metadata and controls
40 lines (33 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
//Contributions
//This part done by David
error_reporting(E_ALL);
ini_set('display_errors', 1); //Show setup errors, used in debugging
require_once 'Config.php'; //Database credentials
require_once 'Database.php'; //DB wrapper class
echo "<pre>Starting database initialization...\n";
try {
$db = new Database(Config::$db); //connect to PostgreSQL
echo "Connected to database successfully.\n";
$db->prepare("DROP TABLE IF EXISTS game_results")->execute(); //remove old table
echo "Dropped game_results (if existed)\n";
$db->prepare("
CREATE TABLE game_results (
id SERIAL PRIMARY KEY,
player_name VARCHAR(50) NOT NULL,
player_hand TEXT NOT NULL, -- CSV of cards
dealer_hand TEXT NOT NULL,
player_total INTEGER NOT NULL,
dealer_total INTEGER NOT NULL,
outcome VARCHAR(10) NOT NULL, -- win / lose / tie
played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
")->execute();
echo "Created game_results\n";
echo "\nDatabase ready – you can now play!\n";
} catch (PDOException $e) {
echo "\nPDO ERROR: " . $e->getMessage() . "\nLine: " . $e->getLine() . "\n"; //More detailed error
} catch (Exception $e) {
echo "\nGENERAL ERROR: " . $e->getMessage() . "\n"; //catch other issues
}
?>