Class Database
License: GNU General Public License v2.0 only **********************************************************************************************
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.
Class: Database
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(); }
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: Now you can use the new object @b $gDb to send a query to the database
Endcode
Par:
Examples To open a connection you can use the settings of the config.php of Admidio.
Located at database.php
Methods summary
public
|
#
__construct( string $engine, string $host, integer $port, string $dbName, string $username = null, string $password = null, array $options = array() )
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. |
private
|
#
setDSNString( string $engine )
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. |
private
|
#
setConnectionOptions( )
Set connection specific options like UTF8 connection. These options should always be set if Admidio connect to a database. |
protected
string
|
|
public
string
|
|
public
string
|
#
getMinimumRequiredVersion( )
Get the minimum required version of the database that is necessary to run Admidio. |
public
string
|
|
public
boolean
|
#
startTransaction( )
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. |
public
boolean
|
#
endTransaction( )
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. |
public
string
|
#
escapeString( string $string )
Escapes special characters within the input string. In contrast to the quote method, the returned string has no quotes around the input string! |
protected
string
|
#
getBacktrace( )
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. |
public
string
|
#
lastInsertId( )
Returns the ID of the unique id column of the last INSERT operation. This method replace the old method Database#insert_id. |
public
PDOStatement
|
#
query( string $sql, boolean $showError = true )
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. |
public
boolean
|
|
public
string[]|array[]
|
#
showColumns( string $table, boolean $showColumnProperties = true )
Methods reads all columns and their properties from the database table. |
public
|
|
public static
string[]
|
|
public
mixed|null
|
#
fetch_array( PDOStatement $pdoStatement = null, integer $fetchType = PDO::FETCH_BOTH )
Fetch a result row as an associative array, a numeric array, or both. |
public
mixed|null
|
|
public
string
|
|
public
integer|null
|
Properties summary
protected
|
$host
|
|
protected
|
$port
|
|
protected
|
$dbName
|
|
protected
|
$username
|
|
protected
|
$password
|
|
protected
|
$options
|
|
protected
|
$dsn
|
|
protected
|
$pdo
|
|
protected
|
$transactions
|
|
protected
|
$pdoStatement
|
|
protected
|
$dbStructure
|
|
protected
|
$fetchArray
|
|
protected
|
$minRequiredVersion
|
|
protected
|
$databaseName
|