db-functions.php 5.48 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
function get_equipment_by_id($pdo, $id) {
76
77
78
	$sql = 'SELECT id, nom FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
79
80
	$equipment_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $equipment_fetch[0];
81
82
83
84
}

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

85
function get_equipment_all_by_id($pdo, $id) {
86
87
88
	$sql = 'SELECT * FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
89
90
	$equipment_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $equipment_fetch[0];
91
92
93
94
}

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

95
function get_category_by_id($pdo, $id) {
96
97
98
	$sql = 'SELECT id, nom FROM categorie WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
99
100
	$category_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $category_fetch[0];
101
102
103
104
}

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

105
106
function get_supplier_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM fournisseurs WHERE id = ?;';
107
108
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
109
110
	$supplier_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $supplier_fetch[0];
111
112
113
114
}

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

115
116
function get_supplier_listshort($pdo) {
	$sql = 'SELECT id, nom FROM fournisseurs;';
117
118
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
119
120
	$supplier_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $supplier_fetch;
121
122
123
124
}

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

125
126
function get_supplier_listall($pdo) {
	$sql = 'SELECT * FROM fournisseurs;';
127
128
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
129
130
	$supplier_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $supplier_fetch;
131
132
133
134
}

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

135
136
137
138
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 ?;';
139
	$stmt = $pdo->prepare($sql);
140
141
142
	$stmt->execute(array($find, $find));
	$supplier_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $supplier_fetch;
143
144
145
146
}

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

147
148
function get_team_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM equipe WHERE id = ?;';
149
150
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
151
152
	$team_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $team_fetch[0];
153
154
155
156
}

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

157
158
function get_team_listshort($pdo) {
	$sql = 'SELECT id, nom FROM equipe;';
159
160
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
161
162
	$team_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $team_fetch;
163
164
165
166
}

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

167
168
function get_team_listall($pdo) {
	$sql = 'SELECT * FROM equipe;';
169
170
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
171
172
	$team_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $team_fetch;
173
174
175
176
}

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

177
178
function get_team_with_appareil($pdo) {
	$sql = 'SELECT id, nom FROM equipe INNER JOIN Listing ON equipe.id = Listing.equipe;';
179
	$stmt = $pdo->prepare($sql);
180
181
182
	$stmt->execute(array($id));
	$team_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $team_fetch;
183
184
185
186
187
188
189
190
191
192
193
194
195
}

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

function get_user_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM users WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$user_fetch =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $user_fetch[0];
}

?>