class.sql.php
7.53 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
defined('ABSPATH') or die('Access denied.');
/**
* MySQL abstract layer for the WPDataTables module
*
* @author cjbug@ya.ru
* @since 10.10.2012
*
* */
class PDTSql {
private $dbhost;
private $dbname;
private $dbuser;
private $dbpass;
private $dbport;
private $link;
private $sqllog;
private $query;
private $result;
private $error;
private $key;
/**
* Constructor
* @param string $sqlhost
* @param string $sqldbname
* @param string $sqluser
* @param string $sqlpassword
*/
function __construct( $sqlhost, $sqldbname, $sqluser, $sqlpassword, $sqlport ) {
$this->dbhost = (((string) $sqlhost)) ? $sqlhost : '';
$this->dbname = (((string) $sqldbname)) ? $sqldbname : '';
$this->dbuser = (((string) $sqluser)) ? $sqluser : '';
$this->dbpass = (((string) $sqlpassword)) ? $sqlpassword : '';
$this->dbport = (((int) $sqlport)) ? $sqlport : '3306';
$this->sqlConnect();
}
/**
* Initializes the connection to the database
* @return boolean
*/
function sqlConnect() {
$this->link = @mysqli_connect( $this->dbhost, $this->dbuser, $this->dbpass, $this->dbname, $this->dbport );
if (!$this->link) {
throw new Exception( mysqli_connect_error() );
} else {
$result = mysqli_select_db($this->link, $this->dbname);
mysqli_query($this->link, 'SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8"; ');
if (!$result) {
throw new Exception( mysqli_error($this->link) );
}
}
return true;
}
/**
* Determines if the connection is established
*/
public function isConnected(){
return !empty( $this->link );
}
/**
* Close the DB connection
* @return boolean
*/
function sqlClose() {
mysqli_close();
return true;
}
/**
* Set the group key
* @param string $key
*/
function setGroupKey($key) {
$this->key = $key;
}
/**
* Clear the group key
*/
function dropGroupKey() {
$this->key = '';
}
/**
* Do a query without expected result (insert, update, delete)
* @param $query
* @param parameters - a single array, or all values
* separated by comma
* @return boolean
*/
function doQuery() {
if ($result = $this->prepare(func_get_args())) {
return true;
} else {
return false;
}
}
/**
* Get a single field value from query result
* @param $query
* @param parameters - a single array, or all values
* separated by comma
* @return boolean Get
*/
function getField() {
if ($result = $this->prepare(func_get_args())) {
$row = mysqli_fetch_row($result);
return $row[0];
} else {
return false;
}
}
/**
* Get a single row from query result
* @param $query
* @param parameters - a single array, or all values
* separated by comma
* @return boolean
*/
function getRow() {
if ($result = $this->prepare(func_get_args())) {
$row = mysqli_fetch_assoc($result);
@mysqli_free_result($result);
return $row;
} else {
return false;
}
}
/**
* Get all results of a query as an indexed array
*
* @return array|bool
*/
function getArray() {
$tmp = null;
if ($result = $this->prepare(func_get_args())) {
while ($row = mysqli_fetch_array($result))
$tmp[] = $row;
@mysqli_free_result($result);
return $tmp;
} else {
return false;
}
}
/**
* Get all results of a query as an assoc array
* @param $query
* @param parameters - a single array, or all values
* separated by comma
* @return boolean
*/
function getAssoc() {
if ($result = $this->prepare(func_get_args())) {
while ($row = mysqli_fetch_assoc($result))
$tmp[] = $row;
@mysqli_free_result($result);
return $tmp;
} else {
return false;
}
}
//[<-- Full version insertion #14 -->]//
/**
* Returns the last MySQL error
*/
function getLastError(){
return mysqli_error( $this->link );
}
/**
* Get the results of a query as an assoc array
* grouped by a provided key
* @return bool
*/
function getAssocGroups() {
$properties = func_get_args();
$key = $properties[0];
array_shift($properties);
if ($result = $this->prepare($properties)) {
while ($row = mysqli_fetch_assoc($result))
$tmp[($row[$key])][] = $row;
@mysqli_free_result($result);
return $tmp;
} else {
return false;
}
}
/**
* Get the results of a query sorted by a provided key
* @return bool
*/
function getAssocByKey() {
$properties = func_get_args();
$key = $properties[0];
array_shift($properties);
if ($result = $this->prepare($properties)) {
while ($row = mysqli_fetch_assoc($result)) {
$tmp[($row[$key])] = $row;
}
@mysqli_free_result($result);
return $tmp;
} else {
return false;
}
}
/**
* Get the results of a query as pairs (id/val)
* @param $query
* @param parameters - a single array, or all values
* separated by comma
* @return boolean
*/
function getPairs() {
if ($result = $this->prepare(func_get_args())) {
while (@$row = mysqli_fetch_row($result))
$tmp[strval($row[0])] = $row[1];
@mysqli_free_result($result);
return $tmp;
} else {
return false;
}
}
//[<-- Full version insertion #13 -->]//
/**
* Prepares the query and the parameters passed
*/
function prepare($properties) {
$q = $properties[0];
unset($properties[0]);
// $q = preg_replace('/\?/', 'x?x', $q);
if (count($properties) > 1) {
foreach ($properties as $p) {
$p = '\'' . mysqli_real_escape_string($this->link, $p) . '\'';
$q = preg_replace('/x\?x/', $p, $q, 1);
}
}elseif( (count($properties) == 1) && (is_array($properties[1])) ){
foreach ($properties[1] as $p) {
$p = '\'' . mysqli_real_escape_string($this->link, $p) . '\'';
$q = preg_replace('/x\?x/', $p, $q, 1);
}
}
$this->query = $q;
$this->error = '';
$result = mysqli_query($this->link, $this->query);
if (mysqli_error($this->link)){ return false; }
while (mysqli_more_results($this->link)){
mysqli_next_result($this->link);
mysqli_store_result($this->link);
}
if (@mysqli_num_rows($result)) {
$row = mysqli_fetch_assoc($result);
if (isset($row['error'])) {
$this->error = $row['error'];
return false;
} else {
mysqli_data_seek($result, 0);
return $result;
}
} else {
return false;
}
}
function getLastInsertId(){
return mysqli_insert_id ( $this->link );
}
}
?>