-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPeopleDB.php
147 lines (118 loc) · 3.72 KB
/
PeopleDB.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class PeopleDB {
protected $mysqli;
const LOCALHOST = '127.0.0.1';
const USER = 'root';
const PASSWORD = 'Password1';
const DATABASE = 'rest_api';
/**
* Constructor de clase
*/
public function __construct()
{
try{
//conexión a base de datos
$this->mysqli = new mysqli(self::LOCALHOST, self::USER, self::PASSWORD, self::DATABASE);
}catch (mysqli_sql_exception $e)
{
//Si no se puede realizar la conexión
http_response_code(500);
exit;
}
}
/**
* obtiene un solo registro dado su ID
* @param int $id identificador unico de registro
* @return Array array con los registros obtenidos de la base de datos
*/
public function getPeople($id=0)
{
$stmt = $this->mysqli->prepare("SELECT * FROM people WHERE id=? ; ");
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$peoples = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $peoples;
}
/**
* obtiene todos los registros de la tabla "people"
* @return Array array con los registros obtenidos de la base de datos
*/
public function getPeoples()
{
$result = $this->mysqli->query('SELECT * FROM people');
$peoples = $result->fetch_all(MYSQLI_ASSOC);
$result->close();
return $peoples;
}
/**
* añade un nuevo registro en la tabla persona
* @param String $name nombre completo de persona
* @return bool TRUE|FALSE
*/
public function insert($name='')
{
$stmt = $this->mysqli->prepare("INSERT INTO people(name) VALUES (?); ");
$stmt->bind_param('s', $name);
$r = $stmt->execute();
$stmt->close();
return $r;
}
/**
* elimina un registro dado el ID
* @param int $id Identificador unico de registro
* @return Bool TRUE|FALSE
*/
public function delete($id=0)
{
$stmt = $this->mysqli->prepare("DELETE FROM people WHERE id = ? ; ");
$stmt->bind_param('s', $id);
$r = $stmt->execute();
$stmt->close();
return $r;
}
/**
* Actualiza registro dado su ID
* @param int $id Description
*/
public function update($id, $newName)
{
if($this->checkID($id))
{
$stmt = $this->mysqli->prepare("UPDATE people SET name=? WHERE id = ? ; ");
$stmt->bind_param('ss', $newName,$id);
$r = $stmt->execute();
$stmt->close();
return $r;
}
return false;
}
/**
* verifica si un ID existe
* @param int $id Identificador unico de registro
* @return Bool TRUE|FALSE
*/
public function checkID($id)
{
$stmt = $this->mysqli->prepare("SELECT * FROM people WHERE ID=?");
$stmt->bind_param("s", $id);
if($stmt->execute())
{
$stmt->store_result();
if ($stmt->num_rows == 1)
{
return true;
}
}
return false;
}
public function getName()
{
$result = $this->mysqli->query('SELECT name FROM people');
$names = $result->fetch_all(MYSQLI_ASSOC);
$result->close();
return $names;
}
}
?>