-
Notifications
You must be signed in to change notification settings - Fork 0
/
createdb
37 lines (35 loc) · 1.19 KB
/
createdb
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
#!/usr/local/bin/php
<?php
require(__DIR__ . '/vendor/autoload.php');
// Load dotenv
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
// Get database credentials
$dbname = getenv('DB_NAME');
$user = getenv('DB_USER');
$pass = getenv('DB_PASS');
// Connect with mysqli to use custom SQL statements
$mysqli = new mysqli("localhost", $user, $pass, $dbname);
// Stop script execution on connect error
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
die();
}
// Drop database first
if (!$mysqli->query("DROP TABLE IF EXISTS `symbols`")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
// Create symbols table
$sql = "CREATE TABLE `symbols` (
`id` int(11) NOT NULL,
`value` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ";
$sql.= "ALTER TABLE `symbols` ADD PRIMARY KEY (`id`); ";
$sql.= "ALTER TABLE `symbols` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; ";
// Print messages
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
} else {
echo "Database created!";
}
?>