-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclass.db.php
124 lines (114 loc) · 3.24 KB
/
class.db.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Mysql connection/query class
* @package gplplanet
* @author Tyler Bell tylerwbell[at]gmail[dot]com
* @copyright (C) 2009-2011 - Tyler Bell
*/
class db {
const CONFIGFILE = "config.ini"; //script and db configuration
public static $db = null; //connection
protected static $instance = null; //db instance
private function __clone(){}
private function __construct() {
//get config
$thisDir = dirname(__FILE__);
$configFile = $thisDir."/".self :: CONFIGFILE; //config file assumed to be in same directory as this file
if (is_readable($configFile)) {
$cfg = parse_ini_file($configFile);
} else {
//can't operate without db configs, so barf and bail
if (is_file($configFile)){
$errMsg = "unreadable config file " . $configFile . "; check file permissions\n";
} else {
$errMsg = "missing config file " . $configFile . "\n";
}
throw new Exception(__METHOD__." ".$errMsg);
return false;
}
//connect
try {
if ($cfg['socket']) {
$this->db = new mysqli($cfg['host'], $cfg['username'], $cfg['password'],$cfg['database'], null, $cfg['socket']);
}
elseif ($cfg['port']) {
$this->db = new mysqli($cfg['host'], $cfg['username'], $cfg['password'],$cfg['database'], $cfg['port'], null);
}
else {
$this->db = new mysqli($cfg['host'], $cfg['username'], $cfg['password'],$cfg['database'], null, null);
}
} catch (Exception $e) {
$errMsg = "MySQL Connect to " . $cfg['host'] . "/" . $cfg['database'] . " failed: %s\n". mysqli_connect_error();
throw new Exception(__METHOD__." ".$e->getMessage().": ".$errMsg);
return false;
}
$this->db->set_charset("utf8"); //set client to utf8
return true;
}
/**
* Singleton management
* @return db object
*/
public static function GetInstance(){
if ( !(self::$instance instanceof db) ){
self::$instance = new db();
}
return self::$instance;
}
/**
* Escapes string
* @param string str
* @return string
*/
public function escapeString($str){
return $this->db->real_escape_string($str);
}
/**
* Query database
* @param string SQL SQL statement
* @return mysql(i) result object
*/
public function query($SQL) {
$result = $this->db->query($SQL);
if ($this->db->error) {
$errMsg = $this->db->error . " (" . $SQL . ")";
throw new Exception(__METHOD__." ".$errMsg);
return false;
}
return $result;
}
/**
* Multi Query database
* @param string SQL SQL statement
* @return mysql(i) result object
*/
public function multiQuery($SQL) {
if ($this->db->multi_query($SQL)) {
$i = 0;
do {
$i++;
} while ($this->db->next_result());
}
if ($this->db->errno) {
$errMsg = $this->db->error . " (" . $SQL . ")";
throw new Exception(__METHOD__." ".$errMsg);
return false;
} else {
return true;
}
/*
$result = $this->db->multi_query($SQL);
if ($this->db->error) {
$errMsg = $this->db->error . " (" . $SQL . ")";
throw new Exception(__METHOD__." ".$errMsg);
return false;
}
//iterate results to ensure that all queries are processed
if ($result = $mysqli->use_result()) {
} else {
return false;
}
*/
}
}
?>