-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.php
80 lines (78 loc) · 2.13 KB
/
database.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
<?php
class Database
{
protected $dbName;
protected $dsn;
protected $error;
protected $stmt;
protected $handler;
public function __construct(string $driverCode, string $host, string $username, string $password, string $dbname, array $driver_options = array())
{
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
foreach ($driver_options as $key => $value) {
$options[$key] = $value;
}
$this->dsn = $driverCode . ":host=" . $host . ";dbname=" . $dbname;
try {
$this->handler = new PDO("mysql:host=$host;dbname=$dbname", $username, $password, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
echo $this->error;
}
}
public function prepare(string $sql)
{
$this->stmt = $this->handler->prepare($sql);
}
public function query(string $sql): PDOStatement
{
$this->stmt = $this->prepare($sql)->execute();
}
public function execute()
{
$this->stmt->execute($data);
}
public function fetchALL(): array
{
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function fetch(): array
{
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function fullExecute(string $sql)
{
try {
$stmt = $this->handler->prepare($sql);
$stmt->execute();
return true;
} catch (Throwable $e) {
throw $e;
}
}
public function fullFetch(string $sql)
{
try {
$stmt = $this->handler->prepare($sql);
$s = $stmt->execute();
if ($s) {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
return array();
}
} catch (Throwable $e) {
throw $e;
}
}
public function prepareAndReturn($sql)
{
$stmt = $this->handler->prepare($sql);
if ($stmt instanceof PDOStatement) {
return $stmt;
}
return false;
}
}