blob: a187aeaad8a0d8e10a04754f151130574affe95d [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
Andreu8192d552019-09-22 16:51:39 +020023 if (!$result = $conn->query($query)) echo $conn->error;
Andreu20cbd1d2019-09-22 00:00:57 +020024
25 // Close the connection
26 $conn->close();
27
28 // Return result of query
29 return $result;
30 }
31
Andreudd6cfb42019-09-22 19:52:39 +020032 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020033 $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()) {
Andreudd6cfb42019-09-22 19:52:39 +020042 if ($getAsObjects) {
43 $user = new User();
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];
51 $user->md5password = $row[7];
52 } else {
53 $user = [];
54 $user["id"] = $row[0];
55 $user["nomcomplet"] = $row[1];
56 $user["curs"] = $row[2];
57 $user["grau"] = $row[3];
58 $user["quimata"] = $row[4];
59 $user["requested"] = $row[5];
60 $user["mort"] = $row[6];
61 $user["md5password"] = $row[7];
62 }
Andreuabbcb7e2019-09-21 18:22:14 +020063
64 array_push($users, $user);
65 }
66 $result->close();
67 } else {
Andreu2a26cd72019-09-22 01:58:23 +020068 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020069 }
Andreuabbcb7e2019-09-21 18:22:14 +020070
71 if ($id > 0) return $users[0];
72 else return $users;
73 }
74?>