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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class Database
* @brief Handle the connection to the database, send all sql statements and handle the returned rows.
*
* This class creates a connection to the database and provides several methods
* to communicate with the database. There are methods to send sql statements
* and to handle the response of the database. This class also supports transactions.
* Just call Database#startTransaction and finish it with Database#endTransaction. If
* you call this multiple times only 1 transaction will be open and it will be closed
* after the last endTransaction was send.
* @par Examples
* To open a connection you can use the settings of the config.php of Admidio.
* @code
* // create object and open connection to database
* try
* {
* $gDb = new Database($gDbType, $g_adm_srv, null, $g_adm_db, $g_adm_usr, $g_adm_pw);
* }
* catch (AdmException $e)
* {
* $e->showText();
* }
* @endcode
* Now you can use the new object @b $gDb to send a query to the database
* @code
* // send sql to database and assign the returned PDOStatement
* $organizationsStatement = $gDb->query('SELECT org_shortname, org_longname FROM adm_organizations');
*
* // now fetch all rows of the returned PDOStatement within one array
* $organizationsList = $organizationsStatement->fetchAll();
*
* // Array with the results:
* // $organizationsList = array(
* // [0] => array(
* // [org_shortname] => 'DEMO'
* // [org_longname] => 'Demo-Organization'
* // )
* // [1] => array(
* // [org_shortname] => 'TEST'
* // [org_longname] => 'Test-Organization'
* // )
*
* // you can also go step by step through the returned PDOStatement
* while ($organizationNames = $organizationsStatement->fetch())
* {
* echo $organizationNames['shortname'].' '.$organizationNames['longname'];
* }
* @endcode
*/
class Database
{
protected $host;
protected $port;
protected $dbName;
protected $username;
protected $password;
protected $options;
protected $dsn;
protected $pdo; ///< The PDO object that handles the communication with the database.
protected $transactions; ///< The transaction marker. If this is > 0 than a transaction is open.
protected $pdoStatement; ///< The PdoStatement object which is needed to handle the return of a query.
protected $dbStructure; ///< array with arrays of every table with their structure
protected $fetchArray;
protected $minRequiredVersion; ///< The minimum required version of this database that is necessary to run Admidio.
protected $databaseName; ///< The name of the database e.g. 'MySQL'
/**
* The constructor will check if a valid engine was set and try to connect to the database.
* If the engine is invalid or the connection not possible an exception will be thrown.
* @param string $engine The database type that is supported from Admidio. @b mysql and @b pgsql are valid values.
* @param string $host The hostname or server where the database is running. e.g. localhost or 127.0.0.1
* @param int $port If you don't use the default port of the database then set your port here.
* @param string $dbName Name of the database you want to connect.
* @param string $username Username to connect to database
* @param string $password Password to connect to database
* @param array $options
* @throws AdmException
*/
public function __construct($engine, $host, $port = null, $dbName, $username = null, $password = null, array $options = array())
{
// for compatibility to old versions accept the string postgresql
if ($engine === 'postgresql')
{
$engine = 'pgsql';
}
$this->host = $host;
$this->port = $port;
$this->dbName = $dbName;
$this->username = $username;
$this->password = $password;
$this->options = $options;
$this->transactions = 0;
$this->fetchArray = array();
$this->dbStructure = array();
$this->minRequiredVersion = '';
$this->databaseName = '';
try
{
$availableDrivers = PDO::getAvailableDrivers();
if (count($availableDrivers) === 0)
{
throw new PDOException('PDO does not support any drivers');
}
if (!in_array($engine, $availableDrivers, true))
{
throw new PDOException('The requested PDO driver '.$engine.' is not supported');
}
$this->setDSNString($engine);
// needed to avoid leaking username, password, ... if a PDOException is thrown
$this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options);
$this->setConnectionOptions();
}
catch (PDOException $e)
{
throw new AdmException($e->getMessage());
}
}
/**
* Create a valid DSN string for the engine that was set through the constructor.
* If no valid engine is set than an exception is thrown.
* @param string $engine The database type that is supported from Admidio. @b mysql and @b pgsql are valid values.
* @throws \PDOException
*/
private function setDSNString($engine)
{
switch ($engine)
{
case 'mysql':
if ($this->port === null)
{
$this->dsn = 'mysql:host='.$this->host.';dbname='.$this->dbName;
}
else
{
$this->dsn = 'mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->dbName;
}
break;
case 'pgsql':
if ($this->port === null)
{
$this->dsn = 'pgsql:host='.$this->host.';dbname='.$this->dbName;
}
else
{
$this->dsn = 'pgsql:host='.$this->host.';port='.$this->port.';dbname='.$this->dbName;
}
break;
default:
throw new PDOException('Engine is not supported by Admidio');
}
}
/**
* Set connection specific options like UTF8 connection.
* These options should always be set if Admidio connect to a database.
*/
private function setConnectionOptions()
{
global $gDebug;
if ($gDebug)
{
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // maybe change to PDO::ERRMODE_WARNING
}
else
{
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}
$this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); // change to false if we convert to prepared statements
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_BOTH); // maybe change in future to PDO::FETCH_ASSOC or PDO::FETCH_OBJ
$this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
// Connect to database with UTF8
$this->query('SET NAMES \'UTF8\'');
if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql')
{
// set ANSI mode, that SQL could be more compatible with other DBs
$this->query('SET SQL_MODE = \'ANSI\'');
// if the server has limited the joins, it can be canceled with this statement
$this->query('SET SQL_BIG_SELECTS = 1');
}
}
/**
* @param string $property Property name of the in use database config
* @return string Returns the value of the chosen property
*/
protected function getPropertyFromDatabaseConfig($property)
{
$xmlDatabases = new SimpleXMLElement(SERVER_PATH.'/adm_program/system/databases.xml', null, true);
$node = $xmlDatabases->xpath('/databases/database[@id="'.$this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME).'"]/'.$property);
return (string) $node[0];
}
/**
* Get the name of the database that is running Admidio.
* @return string Returns a string with the name of the database e.g. 'MySQL' or 'PostgreSQL'
*/
public function getName()
{
if ($this->databaseName === '')
{
$this->databaseName = $this->getPropertyFromDatabaseConfig('name');
}
return $this->databaseName;
}
/**
* Get the minimum required version of the database that is necessary to run Admidio.
* @return string Returns a string with the minimum required database version e.g. '5.0.1'
*/
public function getMinimumRequiredVersion()
{
if ($this->minRequiredVersion === '')
{
$this->minRequiredVersion = $this->getPropertyFromDatabaseConfig('minversion');
}
return $this->minRequiredVersion;
}
/**
* Get the version of the connected database.
* @return string Returns a string with the database version e.g. '5.5.8'
*/
public function getVersion()
{
$versionStatement = $this->query('SELECT version()');
$version = $versionStatement->fetchColumn();
if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql')
{
// the string (PostgreSQL 9.0.4, compiled by Visual C++ build 1500, 64-bit) must be separated
$versionArray = explode(',', $version);
$versionArray2 = explode(' ', $versionArray[0]);
return $versionArray2[1];
}
return $version;
}
/**
* Start a transaction if no open transaction exists. If you call this multiple times
* only 1 transaction will be open and it will be closed after the last endTransaction was send.
* @return bool
* @see Database#endTransaction
* @see Database#rollback
*/
public function startTransaction()
{
global $gDebug;
// If we are within a transaction we will not open another one,
// but enclose the current one to not loose data (prevening auto commit)
if ($this->transactions > 0)
{
++$this->transactions;
return true;
}
// if debug mode then log all sql statements
if ($gDebug)
{
error_log('START TRANSACTION');
}
$result = $this->pdo->beginTransaction();
if (!$result)
{
$this->showError();
// => EXIT
}
$this->transactions = 1;
return $result;
}
/**
* The method will commit an open transaction to the database. If the
* transaction counter is greater 1 than only the counter will be
* decreased and no commit will performed.
* @return bool Returns @b true if the commit was successful otherwise @b false
* @see Database#startTransaction
* @see Database#rollback
*/
public function endTransaction()
{
global $gDebug;
// if there is no open transaction then do nothing and return
if ($this->transactions === 0)
{
return true;
}
// If there was a previously opened transaction we do not commit yet...
// but count back the number of inner transactions
if ($this->transactions > 1)
{
--$this->transactions;
return true;
}
// if debug mode then log all sql statements
if ($gDebug)
{
error_log('COMMIT');
}
$result = $this->pdo->commit();
if (!$result)
{
$this->showError();
// => EXIT
}
$this->transactions = 0;
return $result;
}
/**
* Escapes special characters within the input string.
* In contrast to the <a href="https://secure.php.net/manual/en/pdo.quote.php">quote</a> method,
* the returned string has no quotes around the input string!
* @param string $string The string to be quoted.
* @return string Returns a quoted string that is theoretically safe to pass into an SQL statement.
* @see <a href="https://secure.php.net/manual/en/pdo.quote.php">PDO::quote</a>
*/
public function escapeString($string)
{
return trim($this->pdo->quote($string), "'");
}
/**
* This method will create an backtrace of the current position in the script. If several
* scripts were called than each script with their position will be listed in the backtrace.
* @return string Returns a string with the backtrace of all called scripts.
*/
protected function getBacktrace()
{
$output = '<div style="font-family: monospace;">';
$backtrace = debug_backtrace();
foreach ($backtrace as $number => $trace)
{
// We skip the first one, because it only shows this file/function
if ($number === 0)
{
continue;
}
// Strip the current directory from path
if (empty($trace['file']))
{
$trace['file'] = '';
}
else
{
$trace['file'] = str_replace(array(SERVER_PATH, '\\'), array('', '/'), $trace['file']);
$trace['file'] = substr($trace['file'], 1);
}
$args = array();
// If include/require/include_once is not called, do not show arguments - they may contain sensible information
if (!in_array($trace['function'], array('include', 'require', 'include_once'), true))
{
unset($trace['args']);
}
else
{
// Path...
if (!empty($trace['args'][0]))
{
$argument = htmlentities($trace['args'][0]);
$argument = str_replace(array(SERVER_PATH, '\\'), array('', '/'), $argument);
$argument = substr($argument, 1);
$args[] = "'{$argument}'";
}
}
$trace['class'] = array_key_exists('class', $trace) ? $trace['class'] : '';
$trace['type'] = array_key_exists('type', $trace) ? $trace['type'] : '';
$output .= '<br />';
$output .= '<strong>FILE:</strong> '.htmlentities($trace['file']).'<br />';
$output .= '<strong>LINE:</strong> '.((!empty($trace['line'])) ? $trace['line'] : '').'<br />';
$output .= '<strong>CALL:</strong> '.htmlentities($trace['class'].$trace['type'].$trace['function']).
'('.(count($args) ? implode(', ', $args) : '').')<br />';
}
$output .= '</div>';
return $output;
}
/**
* Returns the ID of the unique id column of the last INSERT operation.
* This method replace the old method Database#insert_id.
* @return string Return ID value of the last INSERT operation.
* @see Database#insert_id
*/
public function lastInsertId()
{
if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql')
{
$lastValStatement = $this->query('SELECT lastval()');
return $lastValStatement->fetchColumn();
}
return $this->pdo->lastInsertId();
}
/**
* Send a sql statement to the database that will be executed. If debug mode is set
* then this statement will be written to the error log. If it's a @b SELECT statement
* then also the number of rows will be logged. If an error occurred the script will
* be terminated and the error with a backtrace will be send to the browser.
* @param string $sql A string with the sql statement that should be executed in database.
* @param bool $showError Default will be @b true and if an error the script will be terminated and
* occurred the error with a backtrace will be send to the browser. If set to
* @b false no error will be shown and the script will be continued.
* @return \PDOStatement For @b SELECT statements an object of <a href="https://secure.php.net/manual/en/class.pdostatement.php">PDOStatement</a> will be returned.
* This should be used to fetch the returned rows. If an error occurred then @b false will be returned.
*/
public function query($sql, $showError = true)
{
global $gDebug;
if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql')
{
$sqlCompare = strtolower($sql);
// prepare the sql statement to be compatible with PostgreSQL
if (strpos($sqlCompare, 'create table') !== false || strpos($sqlCompare, 'alter table') !== false)
{
if (strpos($sqlCompare, 'create table') !== false)
{
// on a create-table-statement if necessary cut existing MySQL table options
$sql = substr($sql, 0, strrpos($sql, ')') + 1);
}
$replaceArray = array(
// PostgreSQL doesn't know unsigned
'unsigned' => '',
// PostgreSQL interprets a boolean as string so transform it to a smallint
'boolean' => 'smallint',
// A blob is in PostgreSQL a bytea datatype
'blob' => 'bytea'
);
$sql = str_replace(array_keys($replaceArray), array_values($replaceArray), $sql);
// Auto_Increment must be replaced with Serial
$posAutoIncrement = strpos($sql, 'AUTO_INCREMENT');
if ($posAutoIncrement > 0)
{
$posInteger = strrpos(substr($sql, 0, $posAutoIncrement), 'integer');
$sql = substr($sql, 0, $posInteger).' serial '.substr($sql, $posAutoIncrement + 14);
}
}
}
// if debug mode then log all sql statements
if ($gDebug)
{
error_log($sql);
}
try
{
$this->fetchArray = array();
$this->pdoStatement = $this->pdo->query($sql);
}
catch (PDOException $e)
{
if($showError)
{
$this->showError();
// => EXIT
}
}
if ($gDebug && strpos(strtoupper($sql), 'SELECT') === 0)
{
// if debug modus then show number of selected rows
error_log('Found rows: '.$this->pdoStatement->rowCount());
}
return $this->pdoStatement;
}
/**
* If there is a open transaction than this method sends a rollback
* to the database and will set the transaction counter to zero.
* @return bool
* @see Database#startTransaction
* @see Database#endTransaction
*/
public function rollback()
{
global $gDebug;
if ($this->transactions > 0)
{
// if debug mode then log all sql statements
if ($gDebug)
{
error_log('ROLLBACK');
}
$result = $this->pdo->rollBack();
if (!$result)
{
$this->showError();
// => EXIT
}
$this->transactions = 0;
return true;
}
return false;
}
/**
* Methods reads all columns and their properties from the database table.
* @param string $table Name of the database table for which the columns should be shown.
* @param bool $showColumnProperties If this is set to @b false only the column names were returned.
* @return string[]|array[] Returns an array with each column and their properties if $showColumnProperties is set to @b true.
* The array has the following format:
* array (
* 'column1' => array (
* 'serial' => '1',
* 'null' => '0',
* 'key' => '0',
* 'type' => 'integer'
* ),
* 'column2' => array (...)
* ...
* )
* TODO https://secure.php.net/manual/en/pdostatement.getcolumnmeta.php
* https://www.postgresql.org/docs/9.5/static/infoschema-columns.html
* https://dev.mysql.com/doc/refman/5.7/en/columns-table.html
*/
public function showColumns($table, $showColumnProperties = true)
{
if (!array_key_exists($table, $this->dbStructure))
{
$columnProperties = array();
if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql')
{
$sql = 'SHOW COLUMNS FROM '.$table;
$columnsStatement = $this->query($sql);
$columnsList = $columnsStatement->fetchAll();
foreach ($columnsList as $properties)
{
$columnProperties[$properties['Field']]['serial'] = 0;
$columnProperties[$properties['Field']]['null'] = 0;
$columnProperties[$properties['Field']]['key'] = 0;
if ($properties['Extra'] === 'auto_increment')
{
$columnProperties[$properties['Field']]['serial'] = 1;
}
if ($properties['Null'] === 'YES')
{
$columnProperties[$properties['Field']]['null'] = 1;
}
if ($properties['Key'] === 'PRI' || $properties['Key'] === 'MUL')
{
$columnProperties[$properties['Field']]['key'] = 1;
}
if (strpos($properties['Type'], 'tinyint(1)') !== false)
{
$columnProperties[$properties['Field']]['type'] = 'boolean';
}
elseif (strpos($properties['Type'], 'smallint') !== false)
{
$columnProperties[$properties['Field']]['type'] = 'smallint';
}
elseif (strpos($properties['Type'], 'int') !== false)
{
$columnProperties[$properties['Field']]['type'] = 'integer';
}
else
{
$columnProperties[$properties['Field']]['type'] = $properties['Type'];
}
}
}
elseif ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql')
{
$sql = 'SELECT column_name, column_default, is_nullable, data_type
FROM information_schema.columns
WHERE table_name = \''.$table.'\'';
$columnsStatement = $this->query($sql);
$columnsList = $columnsStatement->fetchAll();
foreach ($columnsList as $properties)
{
$columnProperties[$properties['column_name']]['serial'] = 0;
$columnProperties[$properties['column_name']]['null'] = 0;
$columnProperties[$properties['column_name']]['key'] = 0;
if (strpos($properties['column_default'], 'nextval') !== false)
{
$columnProperties[$properties['column_name']]['serial'] = 1;
}
if ($properties['is_nullable'] === 'YES')
{
$columnProperties[$properties['column_name']]['null'] = 1;
}
/*if ($properties['Key'] === 'PRI' || $properties['Key'] === 'MUL')
{
$columnProperties[$properties['column_name']]['key'] = 1;
}*/
if (strpos($properties['data_type'], 'timestamp') !== false)
{
$columnProperties[$properties['column_name']]['type'] = 'timestamp';
}
elseif (strpos($properties['data_type'], 'time') !== false)
{
$columnProperties[$properties['column_name']]['type'] = 'time';
}
else
{
$columnProperties[$properties['column_name']]['type'] = $properties['data_type'];
}
}
}
// safe array with table structure in class array
$this->dbStructure[$table] = $columnProperties;
}
if ($showColumnProperties)
{
// returns all columns with their properties of the table
return $this->dbStructure[$table];
}
// returns only the column names of the table.
$tableColumns = array();
foreach ($this->dbStructure[$table] as $columnName => $columnProperties)
{
$tableColumns[] = $columnName;
}
return $tableColumns;
}
/**
* Display the error code and error message to the user if a database error occurred.
* The error must be read by the child method. This method will call a backtrace so
* you see the script and specific line in which the error occurred.
* @return void Will exit the script and returns a html output with the error information.
*/
public function showError()
{
global $g_root_path, $gMessage, $gPreferences, $gCurrentOrganization, $gDebug, $gL10n;
$backtrace = $this->getBacktrace();
// Rollback on open transaction
if ($this->transactions > 0)
{
$this->pdo->rollBack();
}
// transform the database error to html
$errorCode = $this->pdo->errorCode();
$errorInfo = $this->pdo->errorInfo();
$htmlOutput = '
<div style="font-family: monospace;">
<p><strong>S Q L - E R R O R</strong></p>
<p><strong>CODE:</strong> '.$errorCode.'</p>
'.$errorInfo[1].'<br /><br />
'.$errorInfo[2].'<br /><br />
<strong>B A C K T R A C E</strong><br />
'.$backtrace.'
</div>';
// in debug mode show error in log file
if ($gDebug)
{
error_log($errorCode.': '.$errorInfo[1]."\n".$errorInfo[2]);
}
// display database error to user
if (isset($gPreferences) && defined('THEME_SERVER_PATH') && !headers_sent())
{
// create html page object
$page = new HtmlPage($gL10n->get('SYS_DATABASE_ERROR'));
$page->addHtml($htmlOutput);
$page->show();
}
else
{
echo $htmlOutput;
}
exit();
}
/**
* Returns an array with all available PDO database drivers of the server.
* @deprecated 3.1.0:4.0.0 Switched to native PDO method.
* @return string[] Returns an array with all available PDO database drivers of the server.
* @see <a href="https://secure.php.net/manual/en/pdo.getavailabledrivers.php">PDO::getAvailableDrivers</a>
*/
public static function getAvailableDBs()
{
return PDO::getAvailableDrivers();
}
/**
* Fetch a result row as an associative array, a numeric array, or both.
* @deprecated 3.1.0:4.0.0 Switched to native PDO method.
* Please use the PHP class <a href="https://secure.php.net/manual/en/class.pdostatement.php">PDOStatement</a>
* and the method <a href="https://secure.php.net/manual/en/pdostatement.fetch.php">fetch</a> instead.
* @param \PDOStatement $pdoStatement An object of the class PDOStatement. This should be set if multiple
* rows where selected and other sql statements are also send to the database.
* @param int $fetchType Set the result type. Can contain @b PDO::FECTH_ASSOC for an associative array,
* @b PDO::FETCH_NUM for a numeric array or @b PDO::FETCH_BOTH (Default).
* @return mixed|null Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
* @see <a href="https://secure.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a>
*/
public function fetch_array(PDOStatement $pdoStatement = null, $fetchType = PDO::FETCH_BOTH)
{
// if pdo statement is committed then fetch this object
if (is_object($pdoStatement))
{
return $pdoStatement->fetch($fetchType);
}
// if no pdo statement was committed then take the one from the last query
elseif (is_object($this->pdoStatement))
{
return $this->pdoStatement->fetch($fetchType);
}
return null;
}
/**
* Fetch a result row as an object.
* @deprecated 3.1.0:4.0.0 Switched to native PDO method.
* Please use methods Database#fetchAll or Database#fetch instead.
* Please use the PHP class <a href="https://secure.php.net/manual/en/class.pdostatement.php">PDOStatement</a>
* and the method <a href="https://secure.php.net/manual/en/pdostatement.fetchobject.php">fetchObject</a> instead.
* @param \PDOStatement $pdoStatement An object of the class PDOStatement. This should be set if multiple
* rows where selected and other sql statements are also send to the database.
* @return mixed|null Returns an object that corresponds to the fetched row and moves the internal data pointer ahead.
* @see <a href="https://secure.php.net/manual/en/pdostatement.fetchobject.php">PDOStatement::fetchObject</a>
*/
public function fetch_object(PDOStatement $pdoStatement = null)
{
// if pdo statement is committed then fetch this object
if (is_object($pdoStatement))
{
return $pdoStatement->fetchObject();
}
// if no pdo statement was committed then take the one from the last query
elseif (is_object($this->pdoStatement))
{
return $this->pdoStatement->fetchObject();
}
return null;
}
/**
* Returns the ID of the unique id column of the last INSERT operation.
* @deprecated 3.1.0:4.0.0 Renamed method to camelCase style.
* Please use methods Database#lastInsertId instead.
* @return string Return ID value of the last INSERT operation.
* @see Database#lastInsertId
*/
public function insert_id()
{
return $this->lastInsertId();
}
/**
* Returns the number of rows of the last executed statement.
* @deprecated 3.1.0:4.0.0 Switched to native PDO method.
* Please use the PHP class <a href="https://secure.php.net/manual/en/class.pdostatement.php">PDOStatement</a>
* and the method <a href="https://secure.php.net/manual/en/pdostatement.rowcount.php">rowCount</a> instead.
* @param \PDOStatement $pdoStatement An object of the class PDOStatement. This should be set if multiple
* rows where selected and other sql statements are also send to the database.
* @return int|null Return the number of rows of the result of the sql statement.
* @see <a href="https://secure.php.net/manual/en/pdostatement.rowcount.php">PDOStatement::rowCount</a>
*/
public function num_rows(PDOStatement $pdoStatement = null)
{
// if pdo statement is committed then fetch this object
if (is_object($pdoStatement))
{
return $pdoStatement->rowCount();
}
// if no pdo statement was committed then take the one from the last query
elseif (is_object($this->pdoStatement))
{
return $this->pdoStatement->rowCount();
}
return null;
}
}