/
CustomPDO.php
85 lines (77 loc) · 1.42 KB
/
CustomPDO.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
81
82
83
84
85
<?php
/**
* Class CustomPDO
*/
class CustomPDO
{
/**
* @var bool
*/
private $debug;
/**
* @var PDO
*/
private $pdo;
/**
* @param $dbDSN
* @param $dbUsername
* @param $dbPassword
* @param bool $debug
*/
public function __construct($dbDSN, $dbUsername, $dbPassword, $debug = false)
{
$this->debug = $debug;
try {
$this->pdo = new PDO($dbDSN, $dbUsername, $dbPassword);
} catch (PDOException $e) {
$this->throwException($e);
}
}
/**
* @param string $sql
* @return array|bool
*/
public function Query($sql = '')
{
$results = false;
try {
if (strlen($sql) > 0) {
$sth = $this->pdo->query($sql, PDO::FETCH_ASSOC);
if ($sth) {
$results = $sth->fetchAll();
}
}
} catch (PDOException $e) {
$this->throwException($e);
}
return $results;
}
/**
* @param string $sql
* @return bool|string
*/
public function create($sql = '')
{
if (strlen($sql) > 0) {
$sth = $this->pdo->query($sql);
if ($sth) {
return $this->pdo->lastInsertId();
}
}
return false;
}
/**
* @param PDOException $exception
*/
public function throwException(PDOException $exception)
{
if ($this->debug) {
echo '<pre>';
print_r(array(
$exception->getMessage( ) , $exception->getCode( )
));
echo '</pre>';
die();
}
}
}