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 |
| 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 |
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()) { |
| 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]; |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 51 | $user->md5password = $row[7]; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 52 | |
| 53 | array_push($users, $user); |
| 54 | } |
| 55 | $result->close(); |
| 56 | } else { |
| 57 | die("Wrong query: " . $query); |
| 58 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 59 | |
| 60 | if ($id > 0) return $users[0]; |
| 61 | else return $users; |
| 62 | } |
| 63 | ?> |