-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPost.php
More file actions
61 lines (50 loc) · 2.06 KB
/
Post.php
File metadata and controls
61 lines (50 loc) · 2.06 KB
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
<?php
Class Post {
private $pdo;
public function __construct($dbname, $host, $user, $senha) {
try {
$this->pdo = new PDO("mysql:dbname=".$dbname.";host=".$host, $user, $senha);
} catch (PDOException $e) {
echo "Erro com banco de dados: ".$e->getMessage();
exit();
} catch (Exception $e) {
echo "Erro genérico: ".$e->getMessage();
exit();
}
}
public function buscarPostagens() {
$res = array();
$cmd = $this->pdo->prepare("SELECT * FROM postagem ORDER BY data_publicacao DESC");
$cmd->execute();
$res = $cmd->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
public function cadastrarPostagem($titulo, $descricao, $conteudo) {
$cmd = $this->pdo->prepare("INSERT INTO postagem (titulo, descricao, conteudo, data_publicacao) VALUES (:t, :d, :c, NOW())");
$cmd->bindValue(":t", $titulo);
$cmd->bindValue(":d", $descricao);
$cmd->bindValue(":c", $conteudo);
$cmd->execute();
}
public function excluirPostagem($id) {
$cmd = $this->pdo->prepare("DELETE FROM postagem WHERE id = :id");
$cmd->bindValue(":id", $id);
$cmd->execute();
}
public function atualizarPostagem($id, $titulo, $descricao, $conteudo) {
$cmd = $this->pdo->prepare("UPDATE postagem SET titulo = :t, descricao = :d, conteudo = :c WHERE id = :id");
$cmd->bindValue(":t", $titulo);
$cmd->bindValue(":d", $descricao);
$cmd->bindValue(":c", $conteudo);
$cmd->bindValue(":id", $id);
$cmd->execute();
}
public function buscarPostagemPorId($id) {
$cmd = $this->pdo->prepare("SELECT * FROM postagem WHERE id = :id");
$cmd->bindValue(":id", $id);
$cmd->execute();
$res = $cmd->fetch(PDO::FETCH_ASSOC);
return $res;
}
}
?>