1 | <?php |
||
7 | class Database { |
||
8 | use Querybuilder; |
||
9 | |||
10 | private $db_type; |
||
11 | private $db_name; |
||
12 | private $db_user; |
||
13 | private $db_pass; |
||
14 | private $db_host; |
||
15 | private $dbc; |
||
16 | |||
17 | |||
18 | |||
19 | //-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
20 | public function __construct($db_type, $db_name, $db_user, $db_pass, $db_host) { |
||
27 | //-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
28 | |||
29 | |||
30 | |||
31 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
32 | /** |
||
33 | * function qui fait la connexion a la bdd ne peu etre appelee que dans la classe |
||
34 | * @return PDO |
||
35 | */ |
||
36 | private function getPdo() { |
||
44 | //-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
||
45 | |||
46 | //-------------------------- FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------// |
||
47 | /** |
||
48 | * effectue une requete en selectr dans la BDD, si ok on renvoit les donnees sinon on renvoi une erreur |
||
49 | * @param $req |
||
50 | * @return array |
||
51 | */ |
||
52 | public function query($req) { |
||
63 | |||
64 | /** |
||
65 | * fonction qui prepare une requete et qui l'envoi, marche pour insert et update et delete |
||
66 | * @param $req -> la req a executer |
||
67 | * @param $value -> le ou les tableaux de valeurs |
||
68 | */ |
||
69 | public function prepare($req, $value) { |
||
78 | |||
79 | /** |
||
80 | * pour savoir si une valeur sur un champ précis existe deja en bdd, renvoi true si vrai |
||
81 | * @param $table |
||
82 | * @param $champ |
||
83 | * @param $value |
||
84 | * @return boolean|null |
||
85 | */ |
||
86 | public function rechercherEgalite($table, $champ, $value, $id_table = null, $id = null) { |
||
87 | if ($id == null) { |
||
88 | $query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value'"); |
||
89 | } |
||
90 | else { |
||
91 | $query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value' AND $id_table != $id"); |
||
92 | } |
||
93 | |||
94 | $nb = 0; |
||
95 | foreach ($query as $obj) { |
||
96 | $nb = $obj["nb"]; |
||
97 | } |
||
98 | |||
99 | if ($nb > 0) { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | return false; |
||
104 | } |
||
105 | //-------------------------- FIN FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------// |
||
106 | public function quote($quote) { |
||
109 | |||
110 | public function lastInsertId() { |
||
113 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.