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 | |
| 15 | function get_users($id = 0) { |
| 16 | $users = []; |
| 17 | |
| 18 | // Define MySQL login variables |
| 19 | $servername = "localhost"; // "fdb22.awardspace.net"; |
| 20 | $username = "root"; // "3155560_users"; |
| 21 | $password = ""; // "btechnoro@fox4news.info"; |
| 22 | |
| 23 | // Create connection |
| 24 | $conn = new mysqli($servername, $username, $password, "pastanaga"); |
| 25 | if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); |
| 26 | $conn->set_charset("utf8"); |
| 27 | |
| 28 | // Do the query |
| 29 | $query = "SELECT * FROM users"; |
| 30 | if ($id > 0) $query .= " WHERE id=".$id; |
| 31 | |
| 32 | // Fetch the information of the user |
| 33 | if ($result = $conn->query($query)) { |
| 34 | while ($row = $result->fetch_row()) { |
| 35 | $user = new User(); |
| 36 | |
| 37 | $user->id = $row[0]; |
| 38 | $user->nomcomplet = $row[1]; |
| 39 | $user->curs = $row[2]; |
| 40 | $user->grau = $row[3]; |
| 41 | $user->quimata = $row[4]; |
| 42 | $user->requested = $row[5]; |
| 43 | $user->mort = $row[6]; |
| 44 | |
| 45 | array_push($users, $user); |
| 46 | } |
| 47 | $result->close(); |
| 48 | } else { |
| 49 | die("Wrong query: " . $query); |
| 50 | } |
| 51 | |
| 52 | // Close connection |
| 53 | $conn->close(); |
| 54 | |
| 55 | if ($id > 0) return $users[0]; |
| 56 | else return $users; |
| 57 | } |
| 58 | ?> |