db-functions.php 7.92 KB
Newer Older
1
2
3
4
<?php

require_once('connect.php');

Gabriel Moreau's avatar
Gabriel Moreau committed
5
// ---------------------------------------------------------------------
6

Gabriel Moreau's avatar
Gabriel Moreau committed
7
// connexion au serveur mySQL
8
9
10
11
12
13
14
15
16
17
18
19
20

function connect_db() {
	try{
		$pdo = new PDO('mysql:host='.GESTEX_DB_SERVER.'; dbname='.GESTEX_DB_DATABASE, GESTEX_DB_USER, GESTEX_DB_PASSWORD);
	}
	catch(PDOException $exception){
		error_log('Connection error: '.$exception->getMessage());
		echo $exception->getMessage();
		return false;
	}
	return $pdo;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
21
// ---------------------------------------------------------------------
22
23
24
25
26
27
28

function query_db($statement) {
	$result   = mysql_query($statement) or die("<pre>\n\nCan't perform query: " . mysql_error() . " \n\n$statement\n\n</pre>");
	$num_rows = numrows_db($result);
	return array($result, $num_rows);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
29
// ---------------------------------------------------------------------
30
31
32
33
34

function numrows_db($result) {
	return @mysql_num_rows($result);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
35
// ---------------------------------------------------------------------
36
37
38
39
40
41
42
43

function result_db($result,$i=-1) {
	if ($i >= 0) {
		@mysql_data_seek($result,$i);
	}
	return mysql_fetch_array($result);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
44
// ---------------------------------------------------------------------
45
46
47
48
49

function last_id_db() {
	return mysql_insert_id();
}

Gabriel Moreau's avatar
Gabriel Moreau committed
50
// ---------------------------------------------------------------------
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

function check_val_in_db($pdo, $table, $col, $value) {
	//teste l'existence de $value dans le champ $col de la table $table
	//echo "check in:".$table.":".$col." for ".$value."<br />";
	$sql = 'SELECT * FROM ? WHERE ? = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($table, $col, $value));
	$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

	///echo "check_val:".numrows_db($reponse)."<br />";
	//renvoie 0 si non trouve
	//renvoie le nbre d'occurences autrement
	if (count($result) > 0)
		return true;
	return false;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
68
// ---------------------------------------------------------------------
69

70
71
72
73
74
75
76
77
function get_datasheet_listall_by_equipment($pdo, $id_equipment) {
	$sql = 'SELECT * FROM datasheet WHERE id_equipment = ?;' ;
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id_equipment));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
78
// ---------------------------------------------------------------------
79

80
81
82
83
84
85
86
87
function get_datasheet_count_by_equipment($pdo, $id_equipment) {
	$sql = 'SELECT COUNT(*) as count FROM datasheet WHERE id_equipment = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id_equipment));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'];
}

Gabriel Moreau's avatar
Gabriel Moreau committed
88
// ---------------------------------------------------------------------
89

90
function get_equipment_by_id($pdo, $id) {
91
92
93
	$sql = 'SELECT id, nom FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
94
95
96
97
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
98
99
}

Gabriel Moreau's avatar
Gabriel Moreau committed
100
// ---------------------------------------------------------------------
101

102
function get_equipment_all_by_id($pdo, $id) {
103
104
105
	$sql = 'SELECT * FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
106
107
108
109
110
111
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
112
// ---------------------------------------------------------------------
113
114
115
116
117

function get_equipment_listshort($pdo) {
	$sql = 'SELECT id, nom FROM Listing;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
118
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
119
	return $result_fetch;
120
121
}

Gabriel Moreau's avatar
Gabriel Moreau committed
122
// ---------------------------------------------------------------------
123

124
function get_category_by_id($pdo, $id) {
125
126
127
	$sql = 'SELECT id, nom FROM categorie WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
128
129
	$category_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $category_fetch[0];
130
131
}

Gabriel Moreau's avatar
Gabriel Moreau committed
132
// ---------------------------------------------------------------------
133

134
135
function get_supplier_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM fournisseurs WHERE id = ?;';
136
137
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
138
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
139
	return $supplier_fetch[0];
140
141
}

Gabriel Moreau's avatar
Gabriel Moreau committed
142
// ---------------------------------------------------------------------
143

144
145
function get_supplier_listshort($pdo) {
	$sql = 'SELECT id, nom FROM fournisseurs;';
146
	$stmt = $pdo->prepare($sql);
147
	$stmt->execute();
148
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
149
	return $supplier_fetch;
150
151
}

Gabriel Moreau's avatar
Gabriel Moreau committed
152
// ---------------------------------------------------------------------
153

154
155
function get_supplier_listall($pdo) {
	$sql = 'SELECT * FROM fournisseurs;';
156
	$stmt = $pdo->prepare($sql);
157
	$stmt->execute();
158
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
159
	return $supplier_fetch;
160
161
}

Gabriel Moreau's avatar
Gabriel Moreau committed
162
// ---------------------------------------------------------------------
163

164
165
166
167
function get_supplier_find($pdo, $find='') {
	if (empty($find) or ($find === true))
		return get_supplier_listall($pdo);
	$sql = 'SELECT * FROM fournisseurs WHERE nom RLIKE ? OR descr RLIKE ?;';
168
	$stmt = $pdo->prepare($sql);
169
	$stmt->execute(array($find, $find));
170
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
171
	return $supplier_fetch;
172
173
}

Gabriel Moreau's avatar
Gabriel Moreau committed
174
// ---------------------------------------------------------------------
175

176
177
function get_team_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM equipe WHERE id = ?;';
178
179
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
180
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
181
	return $team_fetch[0];
182
183
}

Gabriel Moreau's avatar
Gabriel Moreau committed
184
// ---------------------------------------------------------------------
185

186
187
function get_team_listshort($pdo) {
	$sql = 'SELECT id, nom FROM equipe;';
188
	$stmt = $pdo->prepare($sql);
189
	$stmt->execute();
190
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
191
	return $team_fetch;
192
193
}

Gabriel Moreau's avatar
Gabriel Moreau committed
194
// ---------------------------------------------------------------------
195

196
197
function get_team_listall($pdo) {
	$sql = 'SELECT * FROM equipe;';
198
	$stmt = $pdo->prepare($sql);
199
	$stmt->execute();
200
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
201
	return $team_fetch;
202
203
}

Gabriel Moreau's avatar
Gabriel Moreau committed
204
// ---------------------------------------------------------------------
205

206
207
function get_team_with_appareil($pdo) {
	$sql = 'SELECT id, nom FROM equipe INNER JOIN Listing ON equipe.id = Listing.equipe;';
208
	$stmt = $pdo->prepare($sql);
209
	$stmt->execute();
210
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
211
	return $team_fetch;
212
213
}

Gabriel Moreau's avatar
Gabriel Moreau committed
214
// ---------------------------------------------------------------------
215
216
217
218
219

function get_user_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM users WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
220
221
222
223
224
225
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
226
// ---------------------------------------------------------------------
227
228
229
230
231
232
233
234
235

function get_user_all_by_id($pdo, $id) {
	$sql = 'SELECT * FROM users WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
236
237
}

Gabriel Moreau's avatar
Gabriel Moreau committed
238
// ---------------------------------------------------------------------
239

240
241
242
243
244
245
246
247
248
249
function get_user_all_by_login($pdo, $login) {
	$sql = 'SELECT * FROM users WHERE loggin = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($login));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
250
// ---------------------------------------------------------------------
251

252
253
254
255
function get_version_by_name($pdo, $name) {
	$sql = 'SELECT version FROM version WHERE name = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($name));
256
	$version_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
257
258
259
260
	if (count($version_fetch) > 0)
		return $version_fetch[0];
	return false;
}
Gabriel Moreau's avatar
Gabriel Moreau committed
261
// ---------------------------------------------------------------------
262
263
264
265
266
267
268
269
270

function set_version_by_name($pdo, $name, $version) {
	$sql = 'INSERT INTO version (name, version) VALUES (?, ?);';
	if (get_version_by_name($pdo, $name))
		$sql = 'UPDATE version SET version = ? WHERE name = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($name, $version));
}

271
?>