summaryrefslogtreecommitdiff
path: root/model/User.php
blob: 469a9a17e863c1b832f1c4a5ee7ac61cd9623b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

class User {
	public $id;
	public $name = 'Unknown';
	public $date = 0;
	public $level = 0;

	function get_by_name($name, $dbc) {
		$sql = "SELECT user_id, user_date, user_level FROM users WHERE user_name = ?";
		$stmt = mysqli_stmt_init($dbc);

		if (!mysqli_stmt_prepare($stmt, $sql)) {
			echo 'Failed to get user: ' . mysqli_error($dbc);
		}

		mysqli_stmt_bind_param($stmt, "s", $name);
		mysqli_stmt_execute($stmt);

		$result = mysqli_stmt_get_result($stmt);

		if (mysqli_num_rows($result) == 0) {
		} else {
			while ($row = mysqli_fetch_assoc($result)) {
				$this->id = $row['user_id'];
				$this->name = $name;
				$this->date = $row['user_date'];
				$this->level = $row['user_level'];
			}
		}

		mysqli_free_result($result);
		mysqli_stmt_close($stmt);
	}

	function get_by_id($id, $dbc) {
		$sql = "SELECT user_name, user_date, user_level FROM users WHERE user_id = " . mysqli_real_escape_string($dbc, $id);
		$result = mysqli_query($dbc, $sql);
	
		if (!$result) {
			echo 'Failed to get user: ' . mysqli_error($dbc);
		}
	
		if (mysqli_num_rows($result) == 0) {
		} else {
			while ($row = mysqli_fetch_assoc($result)) {
				$this->id = $id;
				$this->name = $row['user_name'];
				$this->date = $row['user_date'];
				$this->level = $row['user_level'];
			}
		}
	
		mysqli_free_result($result);
	}

}