wb-2_10_x / branches / main / framework / class.database.php @ 6
1 |
<?php
|
---|---|
2 |
/**
|
3 |
*
|
4 |
* @category framework
|
5 |
* @package database
|
6 |
* @copyright WebsiteBaker Org. e.V.
|
7 |
* @link http://websitebaker.org/
|
8 |
* @license http://www.gnu.org/licenses/gpl.html
|
9 |
* @platform WebsiteBaker 2.8.3
|
10 |
* @requirements PHP 5.3.6 and higher
|
11 |
* @version $Id: class.database.php 6 2017-08-28 00:03:54Z Manuela $
|
12 |
* @filesource $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/framework/class.database.php $
|
13 |
* @lastmodified $Date: 2017-08-28 02:03:54 +0200 (Mon, 28 Aug 2017) $
|
14 |
*
|
15 |
*/
|
16 |
/*
|
17 |
Database class
|
18 |
This class will be used to interface between the database
|
19 |
and the Website Baker code
|
20 |
*/
|
21 |
|
22 |
|
23 |
define('DATABASE_CLASS_LOADED', true); |
24 |
// define the old mysql consts for Backward compatibility
|
25 |
if (!defined('MYSQL_ASSOC')) { |
26 |
define('MYSQL_ASSOC', 1); |
27 |
define('MYSQL_NUM', 2); |
28 |
define('MYSQL_BOTH', 3); |
29 |
define('MYSQL_CLIENT_COMPRESS', 32); |
30 |
define('MYSQL_CLIENT_IGNORE_SPACE', 256); |
31 |
define('MYSQL_CLIENT_INTERACTIVE', 1024); |
32 |
define('MYSQL_CLIENT_SSL', 2048); |
33 |
} |
34 |
|
35 |
class database { |
36 |
|
37 |
private $db_handle = null; // readonly from outside |
38 |
private $db_name = ''; |
39 |
private $sTablePrefix = ''; |
40 |
private $connected = false; |
41 |
private $sCharset = 'utf8mb4'; |
42 |
private $sCollation = 'utf8mb4_unicode_ci'; |
43 |
private $error = ''; |
44 |
private $error_no = array(); |
45 |
private $error_type = ''; |
46 |
private $message = array(); |
47 |
private $sActionFile = ''; |
48 |
|
49 |
|
50 |
// Set DB_URL
|
51 |
function __construct($url = '') |
52 |
{ |
53 |
// Connect to database
|
54 |
if (!$this->connect()) { |
55 |
throw new DatabaseException($this->get_error()); |
56 |
} |
57 |
$this->sTablePrefix = TABLE_PREFIX; |
58 |
} |
59 |
|
60 |
// Connect to the database DB_CHARSET
|
61 |
function connect() |
62 |
{ |
63 |
$aTmp = preg_split( |
64 |
'/[^a-z0-9]/i',
|
65 |
strtolower(preg_replace('/[^a-z0-9_]/i', '', (defined('DB_CHARSET') ? DB_CHARSET : 'utf8mb4_unicode_ci'))) |
66 |
); |
67 |
$this->sCharset = $aTmp[0]; |
68 |
$this->sCollation = implode('_', $aTmp); |
69 |
$port = defined('DB_PORT') ? DB_PORT : ini_get('mysqli.default_port'); |
70 |
if (!($this->db_handle = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, $port))) { |
71 |
$this->connected = false; |
72 |
$this->error = mysqli_connect_error();
|
73 |
} else {
|
74 |
if ($this->sCharset) { |
75 |
mysqli_query($this->db_handle, 'SET NAMES '.$this->sCharset); |
76 |
mysqli_set_charset($this->db_handle, $this->sCharset); |
77 |
} |
78 |
$this->db_name = DB_NAME; |
79 |
$this->connected = true; |
80 |
} |
81 |
return $this->connected; |
82 |
} |
83 |
|
84 |
// Disconnect from the database
|
85 |
function disconnect() |
86 |
{ |
87 |
if($this->connected==true) { |
88 |
mysqli_close(); |
89 |
return true; |
90 |
} else {
|
91 |
return false; |
92 |
} |
93 |
} |
94 |
|
95 |
// Run a query
|
96 |
function query($statement) |
97 |
{ |
98 |
$mysql = new mysql($this->db_handle); |
99 |
$mysql->query($statement); |
100 |
$this->set_error($mysql->error()); |
101 |
if($mysql->error()) { |
102 |
return null; |
103 |
} else {
|
104 |
return $mysql; |
105 |
} |
106 |
} |
107 |
|
108 |
// Gets the first column of the first row
|
109 |
function get_one( $statement ) |
110 |
{ |
111 |
$fetch_row = mysqli_fetch_array(mysqli_query($this->db_handle, $statement) ); |
112 |
$result = $fetch_row[0]; |
113 |
$this->set_error(null); |
114 |
if(mysqli_error($this->db_handle)) { |
115 |
$this->set_error(mysqli_error($this->db_handle)); |
116 |
return null; |
117 |
} else {
|
118 |
return $result; |
119 |
} |
120 |
} |
121 |
|
122 |
// Set the DB error
|
123 |
function set_error($message = null) |
124 |
{ |
125 |
$this->error = $message; |
126 |
$this->error_type = 'unknown'; |
127 |
if ($message!=''){ |
128 |
} |
129 |
} |
130 |
|
131 |
// Return true if there was an error
|
132 |
function is_error() |
133 |
{ |
134 |
return (!empty($this->error)) ? true : false; |
135 |
} |
136 |
|
137 |
// Return the error
|
138 |
function get_error() |
139 |
{ |
140 |
return $this->error; |
141 |
} |
142 |
// Return the errno
|
143 |
function get_errno() |
144 |
{ |
145 |
return $this->is_error() ? mysqli_errno($this->db_handle) : 0; |
146 |
} |
147 |
/**
|
148 |
* default Getter for some properties
|
149 |
* @param string $sPropertyName
|
150 |
* @return mixed NULL on error or missing property
|
151 |
*/
|
152 |
public function __get($sPropertyName) |
153 |
{ |
154 |
switch ($sPropertyName): |
155 |
case 'db_handle': |
156 |
case 'DbHandle': |
157 |
$retval = $this->db_handle; |
158 |
break;
|
159 |
case 'db_name': |
160 |
case 'DbName': |
161 |
$retval = $this->db_name; |
162 |
break;
|
163 |
case 'sTablePrefix': |
164 |
case 'TablePrefix': |
165 |
$retval = $this->sTablePrefix; |
166 |
break;
|
167 |
default:
|
168 |
$retval = null; |
169 |
break;
|
170 |
endswitch;
|
171 |
return $retval; |
172 |
} // __get()
|
173 |
/**
|
174 |
* Escapes special characters in a string for use in an SQL statement
|
175 |
* @param string $unescaped_string
|
176 |
* @return string
|
177 |
*/
|
178 |
public function escapeString($unescaped_string) |
179 |
{ |
180 |
return mysqli_real_escape_string($this->db_handle, $unescaped_string); |
181 |
} |
182 |
/**
|
183 |
* Last inserted Id
|
184 |
* @return bool|int false on error, 0 if no record inserted
|
185 |
*/
|
186 |
public function getLastInsertId() |
187 |
{ |
188 |
return mysqli_insert_id($this->db_handle); |
189 |
} |
190 |
|
191 |
/*
|
192 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
193 |
* @param string $field_name: name of the field to seek for
|
194 |
* @return bool: true if field exists
|
195 |
*/
|
196 |
public function field_exists($table_name, $field_name) |
197 |
{ |
198 |
$bRetval = false; |
199 |
$aMatches = array(); |
200 |
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` '; |
201 |
if (($oQuery = $this->query($sql))) { |
202 |
while (($aRecord = $oQuery->fetchRow(MYSQLI_ASSOC))) { |
203 |
$aMatches[] = $aRecord['Field']; |
204 |
} |
205 |
$bRetval = in_array($field_name, $aMatches); |
206 |
} |
207 |
return $bRetval; |
208 |
} |
209 |
|
210 |
/*
|
211 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
212 |
* @param string $index_name: name of the index to seek for
|
213 |
* @return bool: true if field exists
|
214 |
*/
|
215 |
public function index_exists($table_name, $index_name, $number_fields = 0) |
216 |
{ |
217 |
$number_fields = intval($number_fields); |
218 |
$keys = 0; |
219 |
$sql = 'SHOW INDEX FROM `'.$table_name.'`'; |
220 |
if (($res_keys = $this->query($sql))) { |
221 |
while (($rec_key = $res_keys->fetchRow(MYSQLI_ASSOC))) { |
222 |
if ($rec_key['Key_name'] == $index_name ) { |
223 |
$keys++;
|
224 |
} |
225 |
} |
226 |
} |
227 |
if ( $number_fields == 0 ) { |
228 |
return ($keys != $number_fields); |
229 |
} else {
|
230 |
return ($keys == $number_fields); |
231 |
} |
232 |
} |
233 |
/*
|
234 |
public function index_exists1($sTableName, $sIndexName, $number_fields = 0){
|
235 |
$sql = 'SHOW INDEX FROM `'.$sTableName.'` WHERE `Column_name`= \''.$sIndexName.'\'';
|
236 |
}
|
237 |
*/
|
238 |
/*
|
239 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
240 |
* @param string $field_name: name of the field to add
|
241 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
242 |
* @return bool: true if successful, otherwise false and error will be set
|
243 |
*/
|
244 |
public function field_add($table_name, $field_name, $description) |
245 |
{ |
246 |
if( !$this->field_exists($table_name, $field_name) ) |
247 |
{ // add new field into a table
|
248 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' '; |
249 |
$query = $this->query($sql); |
250 |
$this->set_error(mysqli_error($this->db_handle)); |
251 |
if( !$this->is_error() ) |
252 |
{ |
253 |
return ( $this->field_exists($table_name, $field_name) ) ? true : false; |
254 |
} |
255 |
}else
|
256 |
{ |
257 |
$this->set_error('field \''.$field_name.'\' already exists'); |
258 |
} |
259 |
return false; |
260 |
} |
261 |
|
262 |
/*
|
263 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
264 |
* @param string $field_name: name of the field to add
|
265 |
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
266 |
* @return bool: true if successful, otherwise false and error will be set
|
267 |
*/
|
268 |
public function field_modify($table_name, $field_name, $description) |
269 |
{ |
270 |
$retval = false; |
271 |
if( $this->field_exists($table_name, $field_name) ) |
272 |
{ // modify a existing field in a table
|
273 |
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description; |
274 |
$retval = ( $this->query($sql) ? true : false); |
275 |
$this->set_error(mysqli_error($this->db_handle)); |
276 |
} |
277 |
return $retval; |
278 |
} |
279 |
|
280 |
/*
|
281 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
282 |
* @param string $field_name: name of the field to remove
|
283 |
* @return bool: true if successful, otherwise false and error will be set
|
284 |
*/
|
285 |
public function field_remove($table_name, $field_name) |
286 |
{ |
287 |
$retval = false; |
288 |
if( $this->field_exists($table_name, $field_name) ) |
289 |
{ // modify a existing field in a table
|
290 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`'; |
291 |
$retval = ( $this->query($sql) ? true : false ); |
292 |
} |
293 |
return $retval; |
294 |
} |
295 |
|
296 |
/*
|
297 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
298 |
* @param string $index_name: name of the new index (empty string for PRIMARY)
|
299 |
* @param string $field_list: comma seperated list of fields for this index
|
300 |
* @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
|
301 |
* @return bool: true if successful, otherwise false and error will be set
|
302 |
*/
|
303 |
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY') |
304 |
{ |
305 |
$retval = false; |
306 |
$field_list = explode(',', (str_replace(' ', '', $field_list))); |
307 |
$number_fields = sizeof($field_list); |
308 |
$field_list = '`'.implode('`,`', $field_list).'`'; |
309 |
$index_name = (($index_type == 'PRIMARY') ? $index_type : $index_name); |
310 |
if ( $this->index_exists($table_name, $index_name, $number_fields) || |
311 |
$this->index_exists($table_name, $index_name)) |
312 |
{ |
313 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
314 |
$sql .= 'DROP INDEX `'.$index_name.'`'; |
315 |
if (!$this->query($sql)) { return false; } |
316 |
} |
317 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
318 |
$sql .= 'ADD '.$index_type.' '; |
319 |
$sql .= (($index_type == 'PRIMARY') ? 'KEY ' : '`'.$index_name.'` '); |
320 |
$sql .= '( '.$field_list.' ); '; |
321 |
if ($this->query($sql)) { $retval = true; } |
322 |
return $retval; |
323 |
} |
324 |
|
325 |
/*
|
326 |
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
327 |
* @param string $field_name: name of the field to remove
|
328 |
* @return bool: true if successful, otherwise false and error will be set
|
329 |
*/
|
330 |
public function index_remove($table_name, $index_name) |
331 |
{ |
332 |
$retval = false; |
333 |
if ($this->index_exists($table_name, $index_name)) { |
334 |
// modify a existing field in a table
|
335 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`'; |
336 |
$retval = ( $this->query($sql) ? true : false ); |
337 |
} |
338 |
return $retval; |
339 |
} |
340 |
|
341 |
public function setSqlImportActionFile ( $sCallingScript ){ |
342 |
$this->sActionFile = $sCallingScript; |
343 |
trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED); |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Import a standard *.sql dump file
|
348 |
* @param string $sSqlDump link to the sql-dumpfile
|
349 |
* @param string $sTablePrefix
|
350 |
* @param mixed $mAction
|
351 |
* (bool)true => upgrade (default)
|
352 |
* (bool)false => install
|
353 |
* or command (install|uninstall|upgrade) as string
|
354 |
* or calling script as string
|
355 |
* @param string $sTblEngine
|
356 |
* @param string $sTblCollation
|
357 |
* @return boolean true if import successful
|
358 |
*/
|
359 |
public function SqlImport( |
360 |
$sSqlDump,
|
361 |
$sTablePrefix = '', |
362 |
$mAction = true, |
363 |
$sTblEngine = 'MyISAM', |
364 |
$sTblCollation = 'utf8mb4_unicode_ci' |
365 |
) { |
366 |
$iCount = 0; |
367 |
$sSqlBuffer = ''; |
368 |
$bRetval = true; |
369 |
$this->error = ''; |
370 |
// detect requested action
|
371 |
if (is_string($mAction)) { |
372 |
// search for valid command string in $mAction
|
373 |
$sAction = strtolower(preg_replace( |
374 |
'/^.*?(uninstall|install|upgrade)(\.[^\.]+)?$/is',
|
375 |
'$1',
|
376 |
$mAction,
|
377 |
-1,
|
378 |
$iCount
|
379 |
)); |
380 |
$sAction = $iCount ? $sAction : 'upgrade'; |
381 |
} else if (is_bool($mAction)) { |
382 |
// on boolean request select true='upgrade' or false='install'
|
383 |
$sAction = $mAction ? 'upgrade' : 'install'; |
384 |
} else {
|
385 |
// select 'upgrade' if no valid command found
|
386 |
$sAction = 'upgrade'; |
387 |
} |
388 |
// extract charset from given collation
|
389 |
$aTmp = preg_split('/_/', $sTblCollation, null, PREG_SPLIT_NO_EMPTY); |
390 |
$sCharset = $aTmp[0]; |
391 |
// define placeholders
|
392 |
$aSearch[] = '/\{TABLE_PREFIX\}/'; /* step 0 */ |
393 |
$aSearch[] = '/\{FIELD_CHARSET\}/'; /* step 1 */ |
394 |
$aSearch[] = '/\{FIELD_COLLATION\}/'; /* step 2 */ |
395 |
$aSearch[] = '/\{TABLE_ENGINE\}/'; /* step 3 */ |
396 |
$aSearch[] = '/\{TABLE_ENGINE=([a-zA-Z_0-9]*)\}/'; /* step 4 */ |
397 |
$aSearch[] = '/\{CHARSET\}/'; /* step 5 */ |
398 |
$aSearch[] = '/\{COLLATION\}/'; /* step 6 */ |
399 |
// define replacements
|
400 |
$aReplace[] = $sTablePrefix; /* step 0 */ |
401 |
$aReplace[] = ' CHARACTER SET {CHARSET}'; /* step 1 */ |
402 |
$aReplace[] = ' COLLATE {COLLATION}'; /* step 2 */ |
403 |
$aReplace[] = ' {TABLE_ENGINE='.$sTblEngine.'}'; /* step 3 */ |
404 |
$aReplace[] = ' ENGINE=$1 DEFAULT CHARSET={CHARSET} COLLATE={COLLATION}'; /* step 4 */ |
405 |
$aReplace[] = $sCharset; /* step 5 */ |
406 |
$aReplace[] = $sTblCollation; /* step 6 */ |
407 |
// read file into an array
|
408 |
if (($aSql = file( $sSqlDump, FILE_SKIP_EMPTY_LINES ))) { |
409 |
if (sizeof($aSql) > 0) { |
410 |
// remove possible BOM from file
|
411 |
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]); |
412 |
// replace placeholders by replacements over the whole file
|
413 |
$aSql = preg_replace($aSearch, $aReplace, $aSql); |
414 |
} else { $aSql = false; } |
415 |
} |
416 |
|
417 |
while ((bool)$aSql) { |
418 |
$sSqlLine = trim(array_shift($aSql)); |
419 |
if (!preg_match('/^[\-\/]+.*/', $sSqlLine)) { |
420 |
$sSqlBuffer .= ' '.$sSqlLine; |
421 |
if ((substr($sSqlBuffer,-1,1) == ';')) { |
422 |
if (
|
423 |
// drop tables on install or uninstall
|
424 |
preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sSqlBuffer) && |
425 |
($sAction == 'install' || $sAction == 'uninstall') |
426 |
) { |
427 |
if (!$this->query($sSqlBuffer)) { |
428 |
$aSql = $bRetval = false; |
429 |
break;
|
430 |
} |
431 |
} else if ( |
432 |
// create and alter tables on install or upgrade
|
433 |
(preg_match('/^\s*CREATE TABLE/siU', $sSqlBuffer) || |
434 |
preg_match('/^\s*ALTER TABLE/siU', $sSqlBuffer)) && |
435 |
($sAction == 'install' || $sAction == 'upgrade') |
436 |
) { |
437 |
if (!$this->query($sSqlBuffer)) |
438 |
{ |
439 |
switch ($this->get_errno()): |
440 |
case 0: // no error |
441 |
case 1060: |
442 |
case 1061: |
443 |
break;
|
444 |
default: // all other errors |
445 |
$aSql = $bRetval = false; |
446 |
break;
|
447 |
endswitch;
|
448 |
} |
449 |
} else if ( |
450 |
// insert default data on install
|
451 |
(preg_match('/^\s*INSERT INTO /siU', $sSqlBuffer)) && |
452 |
( $sAction == 'install' ) |
453 |
) { |
454 |
if (!$this->query($sSqlBuffer)) { |
455 |
$aSql = $bRetval = false; |
456 |
break;
|
457 |
} |
458 |
} |
459 |
// clear buffer for next statement
|
460 |
$sSqlBuffer = ''; |
461 |
} |
462 |
} |
463 |
} |
464 |
return $bRetval; |
465 |
} |
466 |
|
467 |
/**
|
468 |
* retuns the type of the engine used for requested table
|
469 |
* @param string $table name of the table, including prefix
|
470 |
* @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
|
471 |
*/
|
472 |
public function getTableEngine($table) |
473 |
{ |
474 |
$retVal = false; |
475 |
$mysqlVersion = mysqli_get_server_info($this->db_handle); |
476 |
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine'; |
477 |
$sql = "SHOW TABLE STATUS FROM " . $this->db_name . " LIKE '" . $table . "'"; |
478 |
if(($result = $this->query($sql))) { |
479 |
if(($row = $result->fetchRow(MYSQLI_ASSOC))) { |
480 |
$retVal = $row[$engineValue]; |
481 |
} |
482 |
} |
483 |
return $retVal; |
484 |
} |
485 |
|
486 |
|
487 |
} /// end of class database
|
488 |
|
489 |
define('MYSQL_SEEK_FIRST', 0); |
490 |
define('MYSQL_SEEK_LAST', -1); |
491 |
define('MYSQLI_SEEK_FIRST', 0); |
492 |
define('MYSQLI_SEEK_LAST', -1); |
493 |
|
494 |
class mysql |
495 |
{ |
496 |
private $db_handle = null; |
497 |
private $result = null; |
498 |
private $error = ''; |
499 |
|
500 |
public function __construct($handle) |
501 |
{ |
502 |
$this->db_handle = $handle; |
503 |
} |
504 |
/**
|
505 |
* query sql statement
|
506 |
* @param string $statement
|
507 |
* @return object
|
508 |
* @throws WbDatabaseException
|
509 |
*/
|
510 |
public function query($sStatement) |
511 |
{ |
512 |
$this->result = @mysqli_query($this->db_handle, $sStatement); |
513 |
if (defined('DEBUG')&& DEBUG && ($this->result === false)) { |
514 |
if (DEBUG) { |
515 |
throw new DatabaseException(mysqli_error($this->db_handle)); |
516 |
} else {
|
517 |
throw new DatabaseException('Error in SQL-Statement'); |
518 |
} |
519 |
} |
520 |
$this->error = mysqli_error($this->db_handle); |
521 |
return $this->result; |
522 |
} |
523 |
|
524 |
// Fetch num rows
|
525 |
public function numRows() |
526 |
{ |
527 |
return mysqli_num_rows($this->result); |
528 |
} |
529 |
|
530 |
// Fetch row $typ = MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
|
531 |
public function fetchRow($typ = MYSQLI_BOTH) |
532 |
{ |
533 |
return mysqli_fetch_array($this->result, $typ); |
534 |
} |
535 |
/**
|
536 |
* fetchAssoc
|
537 |
* @return array with assotiative indexes
|
538 |
* @description get current record and increment pointer
|
539 |
*/
|
540 |
public function fetchAssoc() |
541 |
{ |
542 |
return mysqli_fetch_assoc($this->result); |
543 |
} |
544 |
/**
|
545 |
* fetchArray
|
546 |
* @param int $iType MYSQL_ASSOC(default) | MYSQL_BOTH | MYSQL_NUM
|
547 |
* @return array of current record
|
548 |
* @description get current record and increment pointer
|
549 |
*/
|
550 |
public function fetchArray($iType = MYSQLI_ASSOC) |
551 |
{ |
552 |
if ($iType < MYSQLI_ASSOC || $iType > MYSQLI_BOTH) { |
553 |
$iType = MYSQLI_ASSOC; |
554 |
} |
555 |
return mysqli_fetch_array($this->result, $iType); |
556 |
} |
557 |
/**
|
558 |
* fetchObject
|
559 |
* @param string $sClassname Name of the class to use. Is no given use stdClass
|
560 |
* @param string $aParams optional array of arguments for the constructor
|
561 |
* @return object
|
562 |
* @description get current record as an object and increment pointer
|
563 |
*/
|
564 |
public function fetchObject($sClassName = 'stdClass', array $aParams = []) |
565 |
{ |
566 |
if ($sClassName === 'stdClass' || !$sClassName) { |
567 |
$oRetval = mysqli_fetch_object($this->result, 'stdClass'); |
568 |
} elseif (class_exists($sClassName)) { |
569 |
$oRetval = mysqli_fetch_object($this->result, $sClassName, $aParams); |
570 |
} else {
|
571 |
throw new DatabaseException('Class <'.$sClassName.'> not available on request of mysqli_fetch_object()'); |
572 |
} |
573 |
return $oRetval; |
574 |
} |
575 |
/**
|
576 |
* fetchAll
|
577 |
* @param int $iType MYSQL_ASSOC(default) | MYSQL_NUM
|
578 |
* @return array of rows
|
579 |
* @description get all records of the result set
|
580 |
*/
|
581 |
public function fetchAll($iType = MYSQLI_ASSOC) |
582 |
{ |
583 |
$iType = $iType != MYSQLI_NUM ? MYSQLI_ASSOC : MYSQLI_NUM; |
584 |
|
585 |
if (function_exists('mysqli_fetch_all')) { # Compatibility layer with PHP < 5.3 |
586 |
$aRetval = mysqli_fetch_all($this->result, $iType); |
587 |
} else {
|
588 |
for ($aRetval = array(); ($aTmp = mysqli_fetch_array($this->result, $iType));) { $aRetval[] = $aTmp; } |
589 |
} |
590 |
return $aRetval; |
591 |
} |
592 |
|
593 |
public function rewind() |
594 |
{ |
595 |
return $this->seekRow(); |
596 |
} |
597 |
|
598 |
public function seekRow( $position = MYSQLI_SEEK_FIRST ) |
599 |
{ |
600 |
$pmax = $this->numRows() - 1; |
601 |
$offset = (($position < 0 || $position > $pmax) ? $pmax : $position); |
602 |
return mysqli_data_seek($this->result, $offset); |
603 |
} |
604 |
|
605 |
// Get error
|
606 |
public function error() |
607 |
{ |
608 |
if (isset($this->error)) { |
609 |
return $this->error; |
610 |
} else {
|
611 |
return null; |
612 |
} |
613 |
} |
614 |
|
615 |
} // end of class mysql
|
616 |
|
617 |
class DatabaseException extends AppException {} |
618 |
|
619 |
/* this function is placed inside this file temporarely until a better place is found */
|
620 |
/* function to update a var/value-pair(s) in table ****************************
|
621 |
* nonexisting keys are inserted
|
622 |
* @param string $table: name of table to use (without prefix)
|
623 |
* @param mixed $key: a array of key->value pairs to update
|
624 |
* or a string with name of the key to update
|
625 |
* @param string $value: a sting with needed value, if $key is a string too
|
626 |
* @return bool: true if any keys are updated, otherwise false
|
627 |
*/
|
628 |
function db_update_key_value($table, $key, $value = '') |
629 |
{ |
630 |
global $database; |
631 |
if( !is_array($key)) |
632 |
{ |
633 |
if( trim($key) != '' ) |
634 |
{ |
635 |
$key = array( trim($key) => trim($value) ); |
636 |
} else {
|
637 |
$key = array(); |
638 |
} |
639 |
} |
640 |
$retval = true; |
641 |
foreach( $key as $index=>$val) |
642 |
{ |
643 |
$index = strtolower($index); |
644 |
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' '; |
645 |
if (intval($database->get_one($sql))>0) |
646 |
{ |
647 |
$sql = 'UPDATE '; |
648 |
$sql_where = 'WHERE `name` = \''.$index.'\''; |
649 |
} else {
|
650 |
$sql = 'INSERT INTO '; |
651 |
$sql_where = ''; |
652 |
} |
653 |
$sql .= '`'.TABLE_PREFIX.$table.'` '; |
654 |
$sql .= 'SET `name` = \''.$index.'\', '; |
655 |
$sql .= '`value` = \''.$val.'\' '.$sql_where; |
656 |
if (!$database->query($sql) ) |
657 |
{ |
658 |
$retval = false; |
659 |
} |
660 |
} |
661 |
return $retval; |
662 |
} |