-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
49 lines (43 loc) · 1.45 KB
/
db.php
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
41
42
43
44
45
46
47
48
49
<?php
function openDb() {
try {
$pdo = new PDO("sqlite:" . dirname(__FILE__) . "/quests.sqlite");
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query("CREATE TABLE IF NOT EXISTS quests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created DATETIME,
name VARCHAR(255),
trigger VARCHAR(255),
value VARCHAR(255),
amount INTEGER
)");
} catch (Exception $e) {
echo "Impossible d'accéder à la base de données SQLite : " . $e->getMessage();
exit();
}
return $pdo;
}
function getQuests() {
$pdo = openDb();
$result = $pdo->query("SELECT * FROM quests");
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
echo "<tr>
<td>$name</td>
<td>$trigger</td>
<td>$value</td>
<td>$amount</td>
</tr>";
}
}
function addQuest(string $name, string $trigger, string $value, int $amount) {
$pdo = openDb();
$request = $pdo->prepare("INSERT INTO quests (created, name, trigger, value, amount) VALUES (DATETIME('NOW'), :name, :trigger, :value, :amount)");
$request->bindValue(":name", $name);
$request->bindValue(":trigger", $trigger);
$request->bindValue(":value", $value);
$request->bindValue(":amount", $amount);
$request->execute();
}
?>