-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-config.php
79 lines (72 loc) · 2.33 KB
/
db-config.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
<?php
// The Database Object
class Database
{
private $connection = null;
public function __construct($dbhost = "", $dbname = "", $username = "", $password = "")
{
try {
$this->connection = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8mb4;", $username, $password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
// Execute Statement
private function executeStatement($statement = "", $parameters = [])
{
try {
$stmt = $this->connection->prepare($statement);
$stmt->execute($parameters);
return $stmt;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
// Insert Row/Rows To Database - INSERT (Create)
public function Insert($statement = "", $parameters = [])
{
try {
$this->executeStatement($statement, $parameters);
return $this->connection->lastInsertId();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
// Select Row/Rows From Database - SELECT (Read)
public function Select($statement = "", $parameters = [])
{
try {
$stmt = $this->executeStatement($statement, $parameters);
return $stmt->fetchAll();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
// Update Row/Rows From Database - UPDATE
public function Update($statement = "", $parameters = [])
{
try {
$this->executeStatement($statement, $parameters);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
// Delete Row/Rows From Database - DELETE
public function Remove($statement = "", $parameters = [])
{
try {
$this->executeStatement($statement, $parameters);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
// Connect To Database
$db = new Database(
"127.0.0.1", // Host Name
"php_crud", // Database Name
"root", // Username
"" // Password
);