blob: ae0cb5beb30569b734031c5d4e013f5a921d87f1 [file] [log] [blame]
Andreuabbcb7e2019-09-21 18:22:14 +02001<?php
2 class User{
3 public $id;
4 public $nomcomplet;
5 public $curs;
6 public $grau;
7 public $quimata;
8
9 public function nom() {
10 $noms = explode(" ", $this->nomcomplet);
11 return $noms[0];
12 }
13 }
14
Andreu20cbd1d2019-09-22 00:00:57 +020015 function query($query) {
Andreuabbcb7e2019-09-21 18:22:14 +020016 // Create connection
Andreu2457e402019-09-22 00:52:41 +020017 $credentials = new Credentials();
18 $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname);
Andreuabbcb7e2019-09-21 18:22:14 +020019 if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
20 $conn->set_charset("utf8");
21
Andreu20cbd1d2019-09-22 00:00:57 +020022 // Execute query and save result
23 $result = $conn->query($query);
24
25 // Close the connection
26 $conn->close();
27
28 // Return result of query
29 return $result;
30 }
31
32 function get_users($id = 0) {
33 $users = [];
34
35 // Prepare the query
Andreuabbcb7e2019-09-21 18:22:14 +020036 $query = "SELECT * FROM users";
37 if ($id > 0) $query .= " WHERE id=".$id;
38
39 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +020040 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +020041 while ($row = $result->fetch_row()) {
42 $user = new User();
43
44 $user->id = $row[0];
45 $user->nomcomplet = $row[1];
46 $user->curs = $row[2];
47 $user->grau = $row[3];
48 $user->quimata = $row[4];
49 $user->requested = $row[5];
50 $user->mort = $row[6];
Andreu20cbd1d2019-09-22 00:00:57 +020051 $user->md5password = $row[7];
Andreuabbcb7e2019-09-21 18:22:14 +020052
53 array_push($users, $user);
54 }
55 $result->close();
56 } else {
57 die("Wrong query: " . $query);
58 }
Andreuabbcb7e2019-09-21 18:22:14 +020059
60 if ($id > 0) return $users[0];
61 else return $users;
62 }
63?>