-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbc.php
73 lines (65 loc) · 2.28 KB
/
dbc.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
<?php
// from: http://php.thedemosite.co.uk/
// version 1.0
class dbc extends PDO
{
protected static $instance;
public function __construct()
{
$options = array(PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '".CHARSET."';" // this command will be executed during every connection to server - suggested by: [email protected]
);
try {
$this->dbconn = new PDO(DBDRIVER.":host=".DBHOST.";port=".DBPORT.";dbname=".DBNAME,DBUSER,DBPASS,$options);
return $this->dbconn;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function reportDBError($msg)
{
if (DEBUG) print_r('<div style="padding:10%;"><h3>'.nl2br($msg).'</h3>(debug ref. 3.9d)</div>');
else
{
if(!session_id()) session_start();
$_SESSION['mysql_errors'] = "\n\nDb error: ".$msg."\n";
}
}
public static function instance()
{
if (!isset(self::$instance)) self::$instance = new self();
return self::$instance;
}
public function prepare($query, $options = NULL) {
try { return $this->dbconn->prepare($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function bindParam($query) {
try { return $this->dbconn->bindParam($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function query($query) {
try {
if ($this->query($query)) return $this->fetchAll();
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); } }
public function execute($result) {//use for insert/update/delete
try { if ($result->execute()) return $result; }
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function executeGetRows($result) {//use to retrieve rows of data
try {
if ($result->execute()) return $result->fetchAll(PDO::FETCH_ASSOC);
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function __clone()
{ //not allowed
}
public function __destruct()
{
$this->dbconn = null;
}
}