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 | // Define MySQL login variables |
Andreu | 09b8b05 | 2019-09-21 21:47:20 +0200 | [diff] [blame] | 17 | $servername = "localhost"; // "andreuhuguet78654.ipagemysql.com"; |
| 18 | $username = "root"; // "andreu"; |
| 19 | $password = ""; // "1234"; |
| 20 | $dbname = "pastanaga"; // "fme_2019"; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 21 | |
| 22 | // Create connection |
Andreu | 09b8b05 | 2019-09-21 21:47:20 +0200 | [diff] [blame] | 23 | $conn = new mysqli($servername, $username, $password, $dbname); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 24 | if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); |
| 25 | $conn->set_charset("utf8"); |
| 26 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame^] | 27 | // Execute query and save result |
| 28 | $result = $conn->query($query); |
| 29 | |
| 30 | // Close the connection |
| 31 | $conn->close(); |
| 32 | |
| 33 | // Return result of query |
| 34 | return $result; |
| 35 | } |
| 36 | |
| 37 | function get_users($id = 0) { |
| 38 | $users = []; |
| 39 | |
| 40 | // Prepare the query |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 41 | $query = "SELECT * FROM users"; |
| 42 | if ($id > 0) $query .= " WHERE id=".$id; |
| 43 | |
| 44 | // Fetch the information of the user |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame^] | 45 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 46 | while ($row = $result->fetch_row()) { |
| 47 | $user = new User(); |
| 48 | |
| 49 | $user->id = $row[0]; |
| 50 | $user->nomcomplet = $row[1]; |
| 51 | $user->curs = $row[2]; |
| 52 | $user->grau = $row[3]; |
| 53 | $user->quimata = $row[4]; |
| 54 | $user->requested = $row[5]; |
| 55 | $user->mort = $row[6]; |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame^] | 56 | $user->md5password = $row[7]; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 57 | |
| 58 | array_push($users, $user); |
| 59 | } |
| 60 | $result->close(); |
| 61 | } else { |
| 62 | die("Wrong query: " . $query); |
| 63 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 64 | |
| 65 | if ($id > 0) return $users[0]; |
| 66 | else return $users; |
| 67 | } |
| 68 | ?> |