-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.class.php
90 lines (84 loc) · 2.28 KB
/
User.class.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Utilizador do chat
*
* @package Chat\User
* @author Sandro Miguel Marques <[email protected]>
* @version v.1.0 (06/04/2016)
* @copyright Copyright (c) 2016, Sandro
*/
class User extends DbConnPDO {
/** @var string|null Query SQL */
private $_sql;
/**
* Construtor
*/
public function __construct() {
parent::__construct();
}
/**
* Verifica se um determinado nickname existe.
*
* @param string $nickname Nickname do utilizador
*
* @return boolean Devolve 'true' se o nickname já existir.
*/
public function checkNicknameExists($nickname) {
try {
$this->_sql = '
SELECT
NIckname
FROM
`'. USERS .'`
WHERE
`Nickname`=:nickname
LIMIT 1
';
$stmt = $this->prepare($this->_sql);
$stmt->bindParam(':nickname', $nickname, PDO::PARAM_STR);
$stmt->execute();
$num_rows = $stmt->rowCount();
if ($num_rows == 0) {
return false;
}
return true;
} catch (Exception $e) {
throw $e;
}
}
/**
* Insere um utilizador da base de dados.
*
* @param string $nickname Nickname
*
* @return boolean Devolve 'true' em caso de sucesso ou 'false' em caso de erro.
*/
public function insert($nickname) {
$this->_sql = 'INSERT INTO `'.USERS.'`
(Nickname)
VALUES
(:nickname)
';
try {
$stmt = $this->prepare($this->_sql);
$stmt->bindParam(':nickname', $nickname, PDO::PARAM_STR);
if ($stmt->execute()) {
$stmt->closeCursor();
return true;
}
$stmt->closeCursor();
return false;
} catch (Exception $e) {
throw $e;
}
}
/**
* @return string Devolve a query SQL.
*/
public function __toString() {
if (is_null($this->_sql)) {
return 'NULL';
}
return $this->_sql;
}
}