1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MySQL Related Functionality |
4
|
|
|
* @author Joe Huss <[email protected]> |
5
|
|
|
* @copyright 2018 |
6
|
|
|
* @package MyAdmin |
7
|
|
|
* @category SQL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MyDb\Mysqli; |
11
|
|
|
|
12
|
|
|
use \MyDb\Generic; |
13
|
|
|
use \MyDb\Db_Interface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Db |
17
|
|
|
* |
18
|
|
|
* @access public |
19
|
|
|
*/ |
20
|
|
|
class Db extends Generic implements Db_Interface { |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $type = 'mysqli'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* alias function of select_db, changes the database we are working with. |
28
|
|
|
* |
29
|
|
|
* @param string $database the name of the database to use |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
1 |
|
public function useDb($database) { |
33
|
1 |
|
$this->selectDb($database); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* changes the database we are working with. |
38
|
|
|
* |
39
|
|
|
* @param string $database the name of the database to use |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
1 |
|
public function selectDb($database) { |
43
|
1 |
|
$this->connect(); |
44
|
1 |
|
mysqli_select_db($this->linkId, $database); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* public: connection management */ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Db::connect() |
51
|
|
|
* @param string $database |
52
|
|
|
* @param string $host |
53
|
|
|
* @param string $user |
54
|
|
|
* @param string $password |
55
|
|
|
* @return int|\mysqli |
56
|
|
|
*/ |
57
|
13 |
|
public function connect($database = '', $host = '', $user = '', $password = '') { |
58
|
|
|
/* Handle defaults */ |
59
|
13 |
|
if ('' == $database) |
60
|
13 |
|
$database = $this->database; |
61
|
13 |
|
if ('' == $host) |
62
|
13 |
|
$host = $this->host; |
63
|
13 |
|
if ('' == $user) |
64
|
13 |
|
$user = $this->user; |
65
|
13 |
|
if ('' == $password) |
66
|
13 |
|
$password = $this->password; |
67
|
|
|
/* establish connection, select database */ |
68
|
13 |
|
if (!is_object($this->linkId)) { |
|
|
|
|
69
|
13 |
|
$this->connectionAttempt++; |
70
|
13 |
|
if ($this->connectionAttempt > 1) |
71
|
3 |
|
error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
72
|
13 |
|
if ($this->connectionAttempt >= $this->maxConnectErrors) { |
73
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
|
|
|
|
74
|
|
|
return 0; |
75
|
|
|
} |
76
|
|
|
//$this->linkId = new mysqli($host, $user, $password, $database); |
77
|
13 |
|
$this->linkId = mysqli_init(); |
78
|
13 |
|
$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
79
|
13 |
|
$this->linkId->real_connect($host, $user, $password, $database); |
80
|
13 |
|
$this->linkId->set_charset($this->characterSet); |
81
|
|
|
/* |
82
|
|
|
* $this->linkId = $this->Link_Init->real_connect($host, $user, $password, $database); |
83
|
|
|
* if ($this->linkId) |
84
|
|
|
* { |
85
|
|
|
* $this->linkId = $this->Link_Init; |
86
|
|
|
* } |
87
|
|
|
*/ |
88
|
13 |
|
if ($this->linkId->connect_errno) { |
89
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
90
|
|
|
return 0; |
91
|
|
|
} |
92
|
|
|
/*if ($this->characterSet != '') { |
93
|
|
|
if ($this->collation != '') |
94
|
|
|
@mysqli_query($this->linkId, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}, {$this->collation} = {$this->collation};", MYSQLI_STORE_RESULT); |
95
|
|
|
else |
96
|
|
|
@mysqli_query($this->linkId, "SET NAMES {$this->characterSet};", MYSQLI_STORE_RESULT); |
97
|
|
|
mysqli_set_charset($this->linkId, $this->characterSet); |
98
|
|
|
}*/ |
99
|
|
|
} |
100
|
13 |
|
return $this->linkId; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* This only affects systems not using persistent connections */ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Db::disconnect() |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
1 |
|
public function disconnect() { |
110
|
1 |
|
$return = is_object($this->linkId) ? $this->linkId->close() : false; |
|
|
|
|
111
|
1 |
|
$this->linkId = 0; |
112
|
1 |
|
return $return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $string |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
1 |
|
public function real_escape($string) { |
120
|
1 |
|
if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) |
|
|
|
|
121
|
|
|
return $this->escape($string); |
122
|
1 |
|
return mysqli_real_escape_string($this->linkId, $string); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $string |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
1 |
|
public function escape($string) { |
130
|
1 |
|
if (function_exists('mysql_escape_string')) |
131
|
|
|
return mysqli_escape_string($string); |
|
|
|
|
132
|
1 |
|
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $string); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Db::dbAddslashes() |
137
|
|
|
* @param mixed $str |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
public function dbAddslashes($str) { |
141
|
1 |
|
if (!isset($str) || $str == '') |
142
|
|
|
return ''; |
143
|
|
|
|
144
|
1 |
|
return addslashes($str); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/* public: discard the query result */ |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Db::free() |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function free() { |
154
|
|
|
if (is_resource($this->queryId)) |
|
|
|
|
155
|
|
|
@mysqli_free_result($this->queryId); |
|
|
|
|
156
|
|
|
$this->queryId = 0; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Db::queryReturn() |
161
|
|
|
* |
162
|
|
|
* Sends an SQL query to the server like the normal query() command but iterates through |
163
|
|
|
* any rows and returns the row or rows immediately or FALSE on error |
164
|
|
|
* |
165
|
|
|
* @param mixed $query SQL Query to be used |
166
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
167
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
168
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
169
|
|
|
*/ |
170
|
1 |
|
public function queryReturn($query, $line = '', $file = '') { |
171
|
1 |
|
$this->query($query, $line, $file); |
172
|
1 |
|
if ($this->num_rows() == 0) { |
173
|
1 |
|
return false; |
174
|
1 |
|
} elseif ($this->num_rows() == 1) { |
175
|
1 |
|
$this->next_record(MYSQLI_ASSOC); |
176
|
1 |
|
return $this->Record; |
177
|
|
|
} else { |
178
|
1 |
|
$out = []; |
179
|
1 |
|
while ($this->next_record(MYSQLI_ASSOC)) |
180
|
1 |
|
$out[] = $this->Record; |
181
|
1 |
|
return $out; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* db:qr() |
187
|
|
|
* |
188
|
|
|
* alias of queryReturn() |
189
|
|
|
* |
190
|
|
|
* @param mixed $query SQL Query to be used |
191
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
192
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
193
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
194
|
|
|
*/ |
195
|
1 |
|
public function qr($query, $line = '', $file = '') { |
196
|
1 |
|
return $this->queryReturn($query, $line, $file); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* creates a prepaired statement from query |
201
|
|
|
* |
202
|
|
|
* @param string $query sql wuery like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
203
|
|
|
* @return int|\MyDb\Mysqli\mysqli_stmt |
|
|
|
|
204
|
|
|
*/ |
205
|
|
|
public function prepare($query) { |
206
|
|
|
if (!$this->connect()) |
207
|
|
|
return 0; |
208
|
|
|
$haltPrev = $this->haltOnError; |
|
|
|
|
209
|
|
|
$this->haltOnError = 'no'; |
210
|
|
|
return mysqli_prepare($this->linkId, $query); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Db::query() |
215
|
|
|
* |
216
|
|
|
* Sends an SQL query to the database |
217
|
|
|
* |
218
|
|
|
* @param mixed $queryString |
219
|
|
|
* @param string $line |
220
|
|
|
* @param string $file |
221
|
|
|
* @return mixed 0 if no query or query id handler, safe to ignore this return |
222
|
|
|
*/ |
223
|
5 |
|
public function query($queryString, $line = '', $file = '') { |
224
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */ |
225
|
|
|
/* The empty query string is passed on from the constructor, |
226
|
|
|
* when calling the class without a query, e.g. in situations |
227
|
|
|
* like these: '$db = new db_Subclass;' |
228
|
|
|
*/ |
229
|
5 |
|
if ($queryString == '') |
230
|
|
|
return 0; |
231
|
5 |
|
if (!$this->connect()) { |
232
|
|
|
return 0; |
233
|
|
|
/* we already complained in connect() about that. */ |
234
|
|
|
} |
235
|
5 |
|
$haltPrev = $this->haltOnError; |
236
|
5 |
|
$this->haltOnError = 'no'; |
237
|
|
|
// New query, discard previous result. |
238
|
5 |
|
if (is_resource($this->queryId)) |
|
|
|
|
239
|
|
|
$this->free(); |
240
|
5 |
|
if ($this->Debug) |
241
|
|
|
printf("Debug: query = %s<br>\n", $queryString); |
242
|
5 |
|
if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) |
243
|
|
|
$this->log($queryString, $line, $file); |
244
|
5 |
|
$tries = 3; |
245
|
5 |
|
$try = 0; |
246
|
5 |
|
$this->queryId = false; |
247
|
5 |
|
while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
248
|
5 |
|
$try++; |
249
|
5 |
|
if ($try > 1) { |
250
|
|
|
@mysqli_close($this->linkId); |
|
|
|
|
251
|
|
|
$this->connect(); |
252
|
|
|
} |
253
|
5 |
|
$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
254
|
5 |
|
$this->Row = 0; |
255
|
5 |
|
$this->Errno = @mysqli_errno($this->linkId); |
256
|
5 |
|
$this->Error = @mysqli_error($this->linkId); |
257
|
5 |
|
if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
258
|
|
|
$email = "MySQLi Error<br>\n".'Query: '.$queryString."<br>\n".'Error #'.$this->Errno.': '.$this->Error."<br>\n".'Line: '.$line."<br>\n".'File: '.$file."<br>\n".(isset($GLOBALS['tf']) ? 'User: '.$GLOBALS['tf']->session->account_id."<br>\n" : ''); |
259
|
|
|
$email .= '<br><br>Request Variables:<br>'.print_r($_REQUEST, true); |
260
|
|
|
$email .= '<br><br>Server Variables:<br>'.print_r($_SERVER, true); |
261
|
|
|
$subject = $_SERVER['HOSTNAME'].' MySQLi Error'; |
262
|
|
|
$headers = ''; |
263
|
|
|
$headers .= 'MIME-Version: 1.0'.PHP_EOL; |
264
|
|
|
$headers .= 'Content-type: text/html; charset=UTF-8'.PHP_EOL; |
265
|
|
|
$headers .= 'From: No-Reply <[email protected]>'.PHP_EOL; |
266
|
|
|
$headers .= 'X-Mailer: Trouble-Free.Net Admin Center'.PHP_EOL; |
267
|
|
|
mail('[email protected]', $subject, $email, $headers); |
268
|
|
|
mail('[email protected]', $subject, $email, $headers); |
269
|
|
|
$this->haltmsg('Invalid SQL: '.$queryString, $line, $file); |
270
|
|
|
} |
271
|
|
|
} |
272
|
5 |
|
$this->haltOnError = $haltPrev; |
273
|
5 |
|
if (null === $this->queryId || $this->queryId === false) |
274
|
|
|
$this->halt('', $line, $file); |
275
|
|
|
|
276
|
|
|
// Will return nada if it fails. That's fine. |
277
|
5 |
|
return $this->queryId; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return array|null|object |
282
|
|
|
*/ |
283
|
|
|
public function fetchObject() { |
284
|
|
|
$this->Record = @mysqli_fetch_object($this->queryId); |
|
|
|
|
285
|
|
|
return $this->Record; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/* public: walk result set */ |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Db::next_record() |
292
|
|
|
* |
293
|
|
|
* @param mixed $resultType |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
4 |
|
public function next_record($resultType = MYSQLI_BOTH) { |
297
|
4 |
|
if ($this->queryId === false) { |
|
|
|
|
298
|
|
|
$this->halt('next_record called with no query pending.'); |
299
|
|
|
return 0; |
300
|
|
|
} |
301
|
|
|
|
302
|
4 |
|
$this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
|
|
|
303
|
4 |
|
++$this->Row; |
304
|
4 |
|
$this->Errno = mysqli_errno($this->linkId); |
|
|
|
|
305
|
4 |
|
$this->Error = mysqli_error($this->linkId); |
|
|
|
|
306
|
|
|
|
307
|
4 |
|
$stat = is_array($this->Record); |
308
|
4 |
|
if (!$stat && $this->autoFree && is_resource($this->queryId)) |
|
|
|
|
309
|
|
|
$this->free(); |
310
|
4 |
|
return $stat; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/* public: position in result set */ |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Db::seek() |
317
|
|
|
* @param integer $pos |
318
|
|
|
* @return int |
319
|
|
|
*/ |
320
|
|
|
public function seek($pos = 0) { |
321
|
|
|
$status = @mysqli_data_seek($this->queryId, $pos); |
|
|
|
|
322
|
|
|
if ($status) { |
323
|
|
|
$this->Row = $pos; |
324
|
|
|
} else { |
325
|
|
|
$this->halt("seek($pos) failed: result has ".$this->num_rows().' rows'); |
326
|
|
|
/* half assed attempt to save the day, |
327
|
|
|
* but do not consider this documented or even |
328
|
|
|
* desirable behaviour. |
329
|
|
|
*/ |
330
|
|
|
@mysqli_data_seek($this->queryId, $this->num_rows()); |
|
|
|
|
331
|
|
|
$this->Row = $this->num_rows; |
|
|
|
|
332
|
|
|
return 0; |
333
|
|
|
} |
334
|
|
|
return 1; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Initiates a transaction |
339
|
|
|
* |
340
|
|
|
* @return bool |
341
|
|
|
*/ |
342
|
13 |
|
public function transactionBegin() { |
343
|
13 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0) |
344
|
|
|
return true; |
345
|
13 |
|
if (!$this->connect()) |
346
|
|
|
return 0; |
347
|
13 |
|
return mysqli_begin_transaction($this->linkId); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Commits a transaction |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
|
|
public function transactionCommit() { |
356
|
|
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) |
357
|
|
|
return true; |
358
|
|
|
return mysqli_commit($this->linkId); |
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Rolls back a transaction |
363
|
|
|
* |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
13 |
|
public function transactionAbort() { |
367
|
13 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) |
368
|
|
|
return true; |
369
|
13 |
|
return mysqli_rollback($this->linkId); |
|
|
|
|
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* This will get the last insert ID created on the current connection. Should only be called after an insert query is |
374
|
|
|
* run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
375
|
|
|
* unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
376
|
|
|
* |
377
|
|
|
* @param string $table |
378
|
|
|
* @param string $field |
379
|
|
|
* @return int|string |
380
|
|
|
*/ |
381
|
|
|
public function getLastInsertId($table, $field) { |
382
|
|
|
if (!isset($table) || $table == '' || !isset($field) || $field == '') |
383
|
|
|
return -1; |
384
|
|
|
|
385
|
|
|
return @mysqli_insert_id($this->linkId); |
|
|
|
|
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/* public: table locking */ |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Db::lock() |
392
|
|
|
* @param mixed $table |
393
|
|
|
* @param string $mode |
394
|
|
|
* @return bool|int|\mysqli_result |
395
|
|
|
*/ |
396
|
|
|
public function lock($table, $mode = 'write') { |
397
|
|
|
$this->connect(); |
398
|
|
|
|
399
|
|
|
$query = 'lock tables '; |
400
|
|
|
if (is_array($table)) { |
401
|
|
|
while (list($key, $value) = each($table)) { |
|
|
|
|
402
|
|
|
if ($key == 'read' && $key != 0) { |
403
|
|
|
$query .= "$value read, "; |
404
|
|
|
} else { |
405
|
|
|
$query .= "$value $mode, "; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
$query = mb_substr($query, 0, -2); |
409
|
|
|
} else { |
410
|
|
|
$query .= "$table $mode"; |
411
|
|
|
} |
412
|
|
|
$res = @mysqli_query($this->linkId, $query); |
|
|
|
|
413
|
|
|
if (!$res) { |
414
|
|
|
$this->halt("lock($table, $mode) failed."); |
415
|
|
|
return 0; |
416
|
|
|
} |
417
|
|
|
return $res; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Db::unlock() |
422
|
|
|
* @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
423
|
|
|
* @return bool|int|\mysqli_result |
424
|
|
|
*/ |
425
|
|
|
public function unlock($haltOnError = true) { |
426
|
|
|
$this->connect(); |
427
|
|
|
|
428
|
|
|
$res = @mysqli_query($this->linkId, 'unlock tables'); |
|
|
|
|
429
|
|
|
if ($haltOnError === true && !$res) { |
430
|
|
|
$this->halt('unlock() failed.'); |
431
|
|
|
return 0; |
432
|
|
|
} |
433
|
|
|
return $res; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/* public: evaluate the result (size, width) */ |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Db::affectedRows() |
440
|
|
|
* @return int |
441
|
|
|
*/ |
442
|
|
|
public function affectedRows() { |
443
|
|
|
return @mysqli_affected_rows($this->linkId); |
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Db::num_rows() |
448
|
|
|
* @return int |
449
|
|
|
*/ |
450
|
3 |
|
public function num_rows() { |
451
|
3 |
|
return @mysqli_num_rows($this->queryId); |
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Db::num_fields() |
456
|
|
|
* @return int |
457
|
|
|
*/ |
458
|
|
|
public function num_fields() { |
459
|
|
|
return @mysqli_num_fields($this->queryId); |
|
|
|
|
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* gets an array of the table names in teh current datase |
464
|
|
|
* |
465
|
|
|
* @return array |
466
|
|
|
*/ |
467
|
1 |
|
public function tableNames() { |
468
|
1 |
|
$return = []; |
469
|
1 |
|
$this->query('SHOW TABLES'); |
470
|
1 |
|
$i = 0; |
471
|
1 |
|
while ($info = $this->queryId->fetch_row()) { |
|
|
|
|
472
|
1 |
|
$return[$i]['table_name'] = $info[0]; |
473
|
1 |
|
$return[$i]['tablespace_name'] = $this->database; |
474
|
1 |
|
$return[$i]['database'] = $this->database; |
475
|
1 |
|
++$i; |
476
|
|
|
} |
477
|
1 |
|
return $return; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Db::createDatabase() |
482
|
|
|
* |
483
|
|
|
* @param string $adminname |
484
|
|
|
* @param string $adminpasswd |
485
|
|
|
* @return void |
486
|
|
|
*/ |
487
|
|
|
public function createDatabase($adminname = '', $adminpasswd = '') { |
488
|
|
|
$currentUser = $this->user; |
489
|
|
|
$currentPassword = $this->password; |
490
|
|
|
$currentDatabase = $this->database; |
491
|
|
|
|
492
|
|
|
if ($adminname != '') { |
493
|
|
|
$this->user = $adminname; |
494
|
|
|
$this->password = $adminpasswd; |
495
|
|
|
$this->database = 'mysql'; |
496
|
|
|
} |
497
|
|
|
$this->disconnect(); |
498
|
|
|
$this->query("CREATE DATABASE $currentDatabase"); |
499
|
|
|
$this->query("grant all on $currentDatabase.* to $currentUser@localhost identified by '{$currentPassword}'"); |
500
|
|
|
$this->disconnect(); |
501
|
|
|
|
502
|
|
|
$this->user = $currentUser; |
503
|
|
|
$this->password = $currentPassword; |
504
|
|
|
$this->database = $currentDatabase; |
505
|
|
|
$this->connect(); |
506
|
|
|
/*return $return; */ |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* @param $result |
513
|
|
|
* @param $row |
514
|
|
|
* @param int|string $field |
515
|
|
|
* @return bool |
516
|
|
|
*/ |
517
|
|
|
function mysqli_result($result, $row, $field = 0) { |
518
|
|
|
if ($result === false) return false; |
519
|
|
|
if ($row >= mysqli_num_rows($result)) return false; |
520
|
|
|
if (is_string($field) && !(mb_strpos($field, '.') === false)) { |
521
|
|
|
$tField = explode('.', $field); |
522
|
|
|
$field = -1; |
523
|
|
|
$tFields = mysqli_fetch_fields($result); |
524
|
|
|
for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) { |
525
|
|
|
if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) { |
526
|
|
|
$field = $id; |
527
|
|
|
break; |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
if ($field == -1) return false; |
531
|
|
|
} |
532
|
|
|
mysqli_data_seek($result, $row); |
533
|
|
|
$line = mysqli_fetch_array($result); |
534
|
|
|
return isset($line[$field]) ? $line[$field] : false; |
535
|
|
|
} |
536
|
|
|
|