-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
78 lines (69 loc) · 2.17 KB
/
db.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
<?php
class Database{
private $dsn = "mysql:host=localhost;dbname=php_oop_crud_level_1";
private $user = "root";
private $pass = "";
public $conn;
public function __construct(){
try{
$this->conn = new PDO($this->dsn, $this->user, $this->pass);
}
catch(Exception $e){
echo $e->getMessage();
}
}
public function insert($data) {
$sql = "INSERT INTO users (first_name, last_name, email, phone) VALUES (:fname, :lname, :email, :phone)";
$stmt = $this->conn->prepare($sql);
$stmt->execute([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'phone' => $data['phone']
]);
return true;
}
public function read(){
$data = array();
$sql = "SELECT * FROM users";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
$data[] = $row;
}
return $data;
}
public function getUserByID($id){
$sql = "SELECT * FROM users WHERE id= :id";
$stmt = $this->conn->prepare($sql);
$stmt->execute(['id'=> $id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
}
public function update($id, $data) {
$sql = "UPDATE users SET first_name = :fname, last_name = :lname, email = :email, phone = :phone WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->execute([
'id' => $id,
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'phone' => $data['phone']
]);
return true;
}
public function delete($id){
$sql = "DELETE FROM users WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->execute(['id' => $id]);
return true;
}
public function totalRowCount(){
$sql = "SELECT * FROM users";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$t_rows = $stmt->rowCount();
return $t_rows;
}
}