-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.php
55 lines (47 loc) · 1.73 KB
/
Connection.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
<?php
class Connection
{
public PDO $pdo;
public function __construct()
{
$this->pdo = new PDO('mysql:server=localhost;dbname=notes', 'root', 'mysqlmepv');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function getNotes()
{
$statement = $this->pdo->prepare("SELECT * FROM notes ORDER BY create_date DESC");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
public function addNote($note)
{
$statement = $this->pdo->prepare("INSERT INTO notes (title, description, create_date)
VALUES (:title, :description, :date)");
$statement->bindValue('title', $note['title']);
$statement->bindValue('description', $note['description']);
$statement->bindValue('date', date('Y-m-d H:i:s'));
return $statement->execute();
}
public function getNoteById($id)
{
$statement = $this->pdo->prepare("SELECT * FROM notes WHERE id = :id");
$statement->bindValue('id', $id);
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
}
public function updateNote($id, $note)
{
$statement = $this->pdo->prepare("UPDATE notes SET title = :title, description = :description WHERE id = :id");
$statement->bindValue('id', $id);
$statement->bindValue('title', $note['title']);
$statement->bindValue('description', $note['description']);
return $statement->execute();
}
public function removeNote($id)
{
$statement = $this->pdo->prepare("DELETE FROM notes WHERE id = :id");
$statement->bindValue('id', $id);
return $statement->execute();
}
}
return new Connection();