blob: 19b914677599fda7041b555301ddff4619630584 [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
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 {
Andreu2a26cd72019-09-22 01:58:23 +020057 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020058 }
Andreuabbcb7e2019-09-21 18:22:14 +020059
60 if ($id > 0) return $users[0];
61 else return $users;
62 }
63?>