-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
90 lines (75 loc) · 2.21 KB
/
Database.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Class de connexion à la BDD
*/
class Database {
private $pdo;
public function __construct()
{
$this->pdo = new PDO("mysql:host=localhost;dbname=devoirs", 'root', '');
$this->app=new App();
}
/**
* Get informations from Db
*
* @param string $statement
* @param boolean $one
*/
public function query ($statement, $one=false)
{
try {
$state = $this->pdo->query($statement, PDO::FETCH_OBJ);
if ($one) {
$data = $state->fetch();
} else {
$data = $state->fetchAll();
}
$this->app->sendData("Informations récupérées", true, $data);
} catch (\Throwable $th) {
$this->app->sendData(" Erreur lors de la récupération des informations");
}
}
/**
* Get informations from Db
*
* @param string $statement
* @param boolean $one
*/
public function queryReturn ($statement, $one=false)
{
try {
$state = $this->pdo->query($statement, PDO::FETCH_OBJ);
if ($one) {
$data = $state->fetch();
} else {
$data = $state->fetchAll();
}
return $data;
} catch (\Throwable $th) {
$this->app->sendData(" Erreur lors de la récupération des informations");
}
}
/**
* Save data in Db
*
* @param string $statement
* @param string $action
* @param array $param
*/
public function prepare($statement, $action, $param = array()){
try {
$state = $this->pdo->prepare($statement);
$state->execute($param);
if ($action === "save") {
$message = "Données enregistrées";
} elseif ($action === "delete"){
$message = "Données supprimées";
} elseif ($action === "put"){
$message = "Données modifiées";
}
$this->app->sendData($message, true);
} catch (\Throwable $th) {
$this->app->sendData(" Erreur lors de l'enregistrement");
}
}
}