db-functions.php 7.53 KB
Newer Older
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
<?php

require_once('connect.php');

//////////////////////////////////////////////////////
// function connect_db(){
// connexion au serveur mySQL
// $connexion= mysql_pconnect(SERVER, USER, PASSWD);

// selection de la base bultaco
// return mysql_select_db(DATABASE, $connexion);
// }

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;
}

// -------------------------------------------------------------

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);
}

// -------------------------------------------------------------

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

// -------------------------------------------------------------

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

// -------------------------------------------------------------

function last_id_db() {
	return mysql_insert_id();
}

// -------------------------------------------------------------

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;
}

// -------------------------------------------------------------

75
76
77
78
79
80
81
82
83
84
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;
}

// -------------------------------------------------------------

85
86
87
88
89
90
91
92
93
94
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'];
}

// -------------------------------------------------------------

95
function get_equipment_by_id($pdo, $id) {
96
97
98
	$sql = 'SELECT id, nom FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
99
100
101
102
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
103
104
105
106
}

// -------------------------------------------------------------

107
function get_equipment_all_by_id($pdo, $id) {
108
109
110
	$sql = 'SELECT * FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
111
112
113
114
115
116
117
118
119
120
121
122
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// -------------------------------------------------------------

function get_equipment_listshort($pdo) {
	$sql = 'SELECT id, nom FROM Listing;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
123
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
124
	return $result_fetch;
125
126
127
128
}

// -------------------------------------------------------------

129
function get_category_by_id($pdo, $id) {
130
131
132
	$sql = 'SELECT id, nom FROM categorie WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
133
134
	$category_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $category_fetch[0];
135
136
137
138
}

// -------------------------------------------------------------

139
140
function get_supplier_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM fournisseurs WHERE id = ?;';
141
142
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
143
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
144
	return $supplier_fetch[0];
145
146
147
148
}

// -------------------------------------------------------------

149
150
function get_supplier_listshort($pdo) {
	$sql = 'SELECT id, nom FROM fournisseurs;';
151
	$stmt = $pdo->prepare($sql);
152
	$stmt->execute();
153
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
154
	return $supplier_fetch;
155
156
157
158
}

// -------------------------------------------------------------

159
160
function get_supplier_listall($pdo) {
	$sql = 'SELECT * FROM fournisseurs;';
161
	$stmt = $pdo->prepare($sql);
162
	$stmt->execute();
163
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
164
	return $supplier_fetch;
165
166
167
168
}

// -------------------------------------------------------------

169
170
171
172
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 ?;';
173
	$stmt = $pdo->prepare($sql);
174
	$stmt->execute(array($find, $find));
175
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
176
	return $supplier_fetch;
177
178
179
180
}

// -------------------------------------------------------------

181
182
function get_team_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM equipe WHERE id = ?;';
183
184
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
185
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
186
	return $team_fetch[0];
187
188
189
190
}

// -------------------------------------------------------------

191
192
function get_team_listshort($pdo) {
	$sql = 'SELECT id, nom FROM equipe;';
193
	$stmt = $pdo->prepare($sql);
194
	$stmt->execute();
195
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
196
	return $team_fetch;
197
198
199
200
}

// -------------------------------------------------------------

201
202
function get_team_listall($pdo) {
	$sql = 'SELECT * FROM equipe;';
203
	$stmt = $pdo->prepare($sql);
204
	$stmt->execute();
205
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
206
	return $team_fetch;
207
208
209
210
}

// -------------------------------------------------------------

211
212
function get_team_with_appareil($pdo) {
	$sql = 'SELECT id, nom FROM equipe INNER JOIN Listing ON equipe.id = Listing.equipe;';
213
	$stmt = $pdo->prepare($sql);
214
	$stmt->execute();
215
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
216
	return $team_fetch;
217
218
219
220
221
222
223
224
}

// -------------------------------------------------------------

function get_user_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM users WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// -------------------------------------------------------------

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;
241
242
}

243
244
245
246
247
248
// -------------------------------------------------------------

function get_version_by_name($pdo, $name) {
	$sql = 'SELECT version FROM version WHERE name = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($name));
249
	$version_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	if (count($version_fetch) > 0)
		return $version_fetch[0];
	return false;
}
// -------------------------------------------------------------

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));
}

264
?>