php class PDO_ extends PDO public function __construct dsn username pa

 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
<?php
class PDO_ extends PDO
{
public function __construct ($dsn, $username, $password)
{
parent :: __construct ($dsn, $username, $password);
//$this -> setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
$this -> setAttribute (PDO :: ATTR_DEFAULT_FETCH_MODE, PDO :: FETCH_ASSOC);
}
function prepare ($sql) {
$stmt = parent :: prepare($sql, array(
PDO :: ATTR_STATEMENT_CLASS => array('PDOStatement_')
));
return $stmt;
}
function query($sql, $params = array()) {
$stmt = $this -> prepare($sql);
$stmt -> execute($params);
return $stmt;
}
function querySingle($sql, $params = array()) {
$stmt = $this -> query($sql, $params);
$stmt -> execute($params);
return $stmt -> fetchColumn(0);
}
function queryFetch($sql, $params = array()) {
$stmt = $this -> query($sql, $params);
$stmt -> execute($params);
return $stmt -> fetch();
}
}
class PDOStatement_ extends PDOStatement {
function execute($params = array()) {
if (func_num_args() == 1) {
$params = func_get_arg(0);
} else {
$params = func_get_args();
}
if (!is_array($params)) {
$params = array($params);
}
parent :: execute($params);
return $this;
}
function fetchSingle() {
return $this -> fetchColumn(0);
}
function fetchAssoc() {
$this -> setFetchMode(PDO :: FETCH_NUM);
$data = array();
while ($row = $this -> fetch()) {
$data[$row[0]] = $row[1];
}
return $data;
}
}
class DB {
public static $dbs;
public function __construct ($config)
{
$this -> getInstance($config);
}
public static function getInstance ($config) {
//
try {
self :: $dbs = new PDO_ ($config['dsn'], $config['user'], $config['password']);
}
catch (PDOException $e) {
// add to log errors
$f = fopen($_SERVER['DOCUMENT_ROOT'] . '/system/errorlog.txt', 'a+');
fputs($f, $e -> getMessage () . "\r\n");
fclose ($f);
exit ('Сайт выключен из-за технических неполадок!');
}
}
}
$array = explode(" ",microtime());
$gen = $array[1] + $array[0];
//
$DB = new DB ($config['dbh']);
DB :: $dbs -> query ("SET NAMES 'utf8'");
?>