Connection
extends PDO
in package
Uses
Singleton
A PDO Database class with additional debugging functions.
By default will report clean sql-errors as notices, sets the encoding to UTF8 and sets the default fetch behavior to FECTH_ASSOC.
Table of Contents
- $instances : array<string|int, static>
- The instances that are accessible by class::instance() [ 'id' => instance, // direct mapping to an instance 'id2' => 'id' // indirect mapping to an instance 'id3' => function () { return new Sington() } // lazy creation of an instance.
- $logger : Logger
- Logs en profiles the executed queries.
- $reportWarnings : bool
- Report MySQL warnings.
- $driver : string
- The \PDO::ATTR_DRIVER_NAME. "mysql" or "sqlite".
- $previousInsertId : int
- Remember the previous insertId when using warnings. (Because "SHOW WARNINGS" query resets the value of lastInsertId() to "0").
- $quotedIdentifiers : array<string|int, mixed>
- Cached quoted identifiers(column and table names) per database driver.
- __construct() : mixed
- Connect to a database.
- __get() : mixed
- Generate warnings for accessing non-existing properties.
- __set() : mixed
- Generate warnings for setting non-existing properties.
- checkWarnings() : mixed
- Report MySQL warnings and notes if any.
- debug() : mixed
- Debug informatie tonen.
- exec() : int|bool
- Execute an SQL statement and return the number of affected rows.
- fetchAll() : array<string|int, mixed>|false
- Execute the query and return the resultset as array.
- fetchRow() : array<string|int, mixed>|false
- Fetch a single row.
- fetchValue() : string|null|false
- Fetch a single value The resultset may contain only 1 record with only 1 column.
- import() : bool
- Import a MySQL dump file.
- instance() : static
- Get the singleton instance.
- lastInsertId() : string
- Returns the ID of the last inserted row or sequence value.
- prepare() : PreparedStatement
- Prepares a statement for execution and returns a PDOStatement object.
- query() : Statement
- Executes an SQL statement, returning a result set as a PDOStatement object.
- quote() : string
- Quotes a string for use in a query.
- quoteIdentifier() : string
- Puts backticks '`' around a column- table or databasename.
- renderLog() : string
- De sql query in het debug_blok overzichtelijk weergeven De keywords van een sql query dik(<b>) maken en de querytijd een kleur geven (rood voor trage queries, orange voor middelmatige en grijs voor snelle queries).
- reportError() : mixed
- Report SQL error A cleaner error than PDO::ERRMODE_WARNING generates would generate.
- defaultInstance() : static
- Create a default instance.
Properties
$instances
The instances that are accessible by class::instance() [ 'id' => instance, // direct mapping to an instance 'id2' => 'id' // indirect mapping to an instance 'id3' => function () { return new Sington() } // lazy creation of an instance.
public
static array<string|int, static>
$instances
= []
].
$logger
Logs en profiles the executed queries.
public
Logger
$logger
$reportWarnings
Report MySQL warnings.
public
bool
$reportWarnings
= true
$driver
The \PDO::ATTR_DRIVER_NAME. "mysql" or "sqlite".
private
string
$driver
$previousInsertId
Remember the previous insertId when using warnings. (Because "SHOW WARNINGS" query resets the value of lastInsertId() to "0").
private
int
$previousInsertId
$quotedIdentifiers
Cached quoted identifiers(column and table names) per database driver.
private
static array<string|int, mixed>
$quotedIdentifiers
= []
Methods
__construct()
Connect to a database.
public
__construct(string $dsn[, string $username = null ][, string $passwd = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $dsn : string
-
The pdo-dsn "mysql:host=localhost" or url: "mysql://root@localhost/my_database?charset=utf-8"
- $username : string = null
- $passwd : string = null
- $options : array<string|int, mixed> = []
Return values
mixed —__get()
Generate warnings for accessing non-existing properties.
public
__get(string $property) : mixed
Parameters
- $property : string
Return values
mixed —__set()
Generate warnings for setting non-existing properties.
public
__set(string $property, mixed $value) : mixed
Parameters
- $property : string
- $value : mixed
Return values
mixed —checkWarnings()
Report MySQL warnings and notes if any.
public
checkWarnings(Sql|string $statement) : mixed
Parameters
- $statement : Sql|string
Return values
mixed —debug()
Debug informatie tonen.
public
debug([bool $popup = true ][, string|null $connection = null ]) : mixed
Parameters
- $popup : bool = true
-
true: Add javascript to open the querylog in a popup.
- $connection : string|null = null
-
Name of the connection a.k.a. $dblink.
Return values
mixed —exec()
Execute an SQL statement and return the number of affected rows.
public
exec(string $statement) : int|bool
Parameters
- $statement : string
-
The SQL statement to prepare and execute.
Tags
Return values
int|bool —fetchAll()
Execute the query and return the resultset as array.
public
fetchAll(string $statement) : array<string|int, mixed>|false
Parameters
- $statement : string
-
The SQL statement
Return values
array<string|int, mixed>|false —fetchRow()
Fetch a single row.
public
fetchRow(string $statement[, bool $allow_empty_results = false ]) : array<string|int, mixed>|false
Parameters
- $statement : string
-
The SQL query
- $allow_empty_results : bool = false
-
true: Suppress the notice when no record is found.
Return values
array<string|int, mixed>|false —fetchValue()
Fetch a single value The resultset may contain only 1 record with only 1 column.
public
fetchValue(string $sql[, bool $allow_empty_results = false ]) : string|null|false
Parameters
- $sql : string
-
The SQL query
- $allow_empty_results : bool = false
-
true: Suppress the notice when no record is found.
Return values
string|null|false —import()
Import a MySQL dump file.
public
import(string $filepath, string &$error_message[, bool $strip_php_comments = false ][, false|callable $progress_callback = false ]) : bool
Parameters
- $filepath : string
-
The sql file.
- $error_message : string
-
Contains the error message if any.
- $strip_php_comments : bool = false
-
Bij true worden de regels met "/" beginnen en eindingen met " /" genegeerd
- $progress_callback : false|callable = false
-
Deze callback word na elke voltooide query aangeroepen met het regelnummer als parameter.
Return values
bool —instance()
Get the singleton instance.
public
static instance([string $identifier = 'default' ]) : static
Parameters
- $identifier : string = 'default'
-
The identifier (string),
Return values
static —lastInsertId()
Returns the ID of the last inserted row or sequence value.
public
lastInsertId([string $name = null ]) : string
Parameters
- $name : string = null
-
[optional]
Tags
Return values
string —prepare()
Prepares a statement for execution and returns a PDOStatement object.
public
prepare(string $statement[, array<string|int, mixed> $driver_options = [] ]) : PreparedStatement
Parameters
- $statement : string
-
The SQL statement to prepare
- $driver_options : array<string|int, mixed> = []
Tags
Return values
PreparedStatement —query()
Executes an SQL statement, returning a result set as a PDOStatement object.
public
query(string $statement[, int|null $mode = null ], mixed ...$args) : Statement
Parameters
- $statement : string
-
The SQL statement to prepare and execute.
- $mode : int|null = null
- $args : mixed
Tags
Return values
Statement —quote()
Quotes a string for use in a query.
public
quote(string $value[, int $parameterType = null ]) : string
Parameters
- $value : string
-
The string to be quoted.
- $parameterType : int = null
-
[optional] Provides a data type hint for drivers that have alternate quoting styles.
Tags
Return values
string —A quoted string that is safe to pass into an SQL statement.
quoteIdentifier()
Puts backticks '`' around a column- table or databasename.
public
quoteIdentifier(string $identifier) : string
Only adds quotes around columnname if needed. (Prevents SQL injection).
Parameters
- $identifier : string
-
A column, table or database-name
Return values
string —renderLog()
De sql query in het debug_blok overzichtelijk weergeven De keywords van een sql query dik(<b>) maken en de querytijd een kleur geven (rood voor trage queries, orange voor middelmatige en grijs voor snelle queries).
public
renderLog(mixed $entry, mixed $meta) : string
Parameters
- $entry : mixed
- $meta : mixed
Return values
string —reportError()
Report SQL error A cleaner error than PDO::ERRMODE_WARNING generates would generate.
public
reportError(string $statement[, mixed $error = null ]) : mixed
Parameters
- $statement : string
- $error : mixed = null
Return values
mixed —defaultInstance()
Create a default instance.
protected
static defaultInstance() : static
Override in the (sub)class to create an instance with default parameters.