Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 1 | <?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 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 15 | function query($query) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 16 | // Create connection |
Andreu | 2457e40 | 2019-09-22 00:52:41 +0200 | [diff] [blame] | 17 | $credentials = new Credentials(); |
| 18 | $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 19 | if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); |
| 20 | $conn->set_charset("utf8"); |
| 21 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 22 | // Execute query and save result |
Andreu | 8192d55 | 2019-09-22 16:51:39 +0200 | [diff] [blame] | 23 | if (!$result = $conn->query($query)) echo $conn->error; |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 24 | |
| 25 | // Close the connection |
| 26 | $conn->close(); |
| 27 | |
| 28 | // Return result of query |
| 29 | return $result; |
| 30 | } |
| 31 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame^] | 32 | function get_users($id = 0, $getAsObjects = true) { |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 33 | $users = []; |
| 34 | |
| 35 | // Prepare the query |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 36 | $query = "SELECT * FROM users"; |
| 37 | if ($id > 0) $query .= " WHERE id=".$id; |
| 38 | |
| 39 | // Fetch the information of the user |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 40 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 41 | while ($row = $result->fetch_row()) { |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame^] | 42 | 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 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 63 | |
| 64 | array_push($users, $user); |
| 65 | } |
| 66 | $result->close(); |
| 67 | } else { |
Andreu | 2a26cd7 | 2019-09-22 01:58:23 +0200 | [diff] [blame] | 68 | die("Query failed: " . $query); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 69 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 70 | |
| 71 | if ($id > 0) return $users[0]; |
| 72 | else return $users; |
| 73 | } |
| 74 | ?> |