z_db
in package
Proxy for all database access. Also holds utility functions
Table of Contents
- $booter : z_framework
- $connectTimeout : int
- $insertId : int
- $lastConnect : int
- $lastHeartbeat : int
- $result : null|bool|mysqli_result
- $conn : mysqli
- $stmt : mysqli_stmt
- __construct() : mixed
- When instanced, a db connection is given as a refrence
- __destruct() : mixed
- Closes the database connection on exit
- assertConnection() : mixed
- checkIfExists() : bool
- Checks if a value exists in a table.
- checkIfUnique() : bool
- Checks if a value is already in a table. Can also ignore a dataset.
- connect() : void
- Make a connection to the database
- countResults() : int
- Returns the number of results in the last query
- countTableEntries() : int
- Returns the number of datasets in a table
- disconnect() : void
- Disconnect from the database
- exec() : z_db
- Executes a query as prepared statement
- getFullTable() : array<string|int, array<string|int, mixed>>
- Selects a full table or specified fields of it and returns the result as two dimensional array
- getInsertId() : int
- Returns the id of the last inserted element
- getTableWhere() : array<string|int, array<string|int, mixed>>
- Selects a full table of specified fields of it filtered with an additional where statement. It returns the result as two dimensional array
- heartbeat() : void
- Run a very lightweight query to keep the connection alive
- mergeAsGroup() : array<string|int, array<string|int, mixed>>
- Converts the result of the last query into a grouped array
- resultToArray() : array<string|int, array<string|int, mixed>>
- Converts the result of the last query into an array and returns it
- resultToLine() : array<string|int, mixed>
- Returns one line of the last query
Properties
$booter
public
z_framework
$booter
Reference to the booter
$connectTimeout
public
int
$connectTimeout
Database connection timeout in seconds. Defaults to 0.5 hours
$insertId
public
int
$insertId
Last insert id
$lastConnect
public
int
$lastConnect
Unix timestamp of the last database connect
$lastHeartbeat
public
int
$lastHeartbeat
Unix timestamp of the last database connect
$result
public
null|bool|mysqli_result
$result
Result of the last query
$conn
private
mysqli
$conn
Connection to the database
$stmt
private
mysqli_stmt
$stmt
Prepared statement object used for queries
Methods
__construct()
When instanced, a db connection is given as a refrence
public
__construct(mixed &$booter) : mixed
Parameters
- $booter : mixed
Return values
mixed —__destruct()
Closes the database connection on exit
public
__destruct() : mixed
Return values
mixed —assertConnection()
public
assertConnection() : mixed
Return values
mixed —checkIfExists()
Checks if a value exists in a table.
public
checkIfExists(string $table, string $field, mixed $value) : bool
Table and field names are inserted unescaped. Check your input.
Parameters
- $table : string
-
Name of the table to check in
- $field : string
-
Name of the field in that a value should exist
- $value : mixed
-
Value to check for
Return values
bool —True when exists
checkIfUnique()
Checks if a value is already in a table. Can also ignore a dataset.
public
checkIfUnique(string $table, string $field, mixed $value[, string $ignoreField = null ][, string $ignoreValue = null ]) : bool
Table and field names are inserted unescaped. Check your input.
Parameters
- $table : string
-
Name of the table to check in
- $field : string
-
Field to check in
- $value : mixed
-
Value to check for
- $ignoreField : string = null
-
field of a dataset to ignore
- $ignoreValue : string = null
-
value of the in the argument before defined field of the dataset to ignore
Return values
bool —True when not exists
connect()
Make a connection to the database
public
connect([string $charset = "utf8mb4" ][, bool $firstConnection = false ]) : void
Parameters
- $charset : string = "utf8mb4"
- $firstConnection : bool = false
-
Determine if this a reconnect
Return values
void —countResults()
Returns the number of results in the last query
public
countResults() : int
Return values
int —Number of results in the last query
countTableEntries()
Returns the number of datasets in a table
public
countTableEntries(string $table) : int
Parameters
- $table : string
-
Name of the table in the database
Return values
int —Number of datasets in the specified table
disconnect()
Disconnect from the database
public
disconnect([bool $forceClose = false ]) : void
Parameters
- $forceClose : bool = false
-
Close the connection regardless of if it seems to be open
Return values
void —exec()
Executes a query as prepared statement
public
exec(string $query) : z_db
Parameters
- $query : string
-
Query written as prepared statement (that thing with the question marks as placeholders)
Return values
z_db —Returning this for chaining
getFullTable()
Selects a full table or specified fields of it and returns the result as two dimensional array
public
getFullTable(string $table[, string $fields = "*" ]) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string = "*"
-
Fields to select. Formatted as in an SQL query ("*", "a, b, c"...)
Return values
array<string|int, array<string|int, mixed>> —A two dimensional array with the results of the select statement
getInsertId()
Returns the id of the last inserted element
public
getInsertId() : int
Return values
int —Id of the last inserted element
getTableWhere()
Selects a full table of specified fields of it filtered with an additional where statement. It returns the result as two dimensional array
public
getTableWhere(string $table[, string $fields = "*" ][, string $where = "" ][, string $types = "" ][, array<string|int, mixed> $values = [] ]) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string = "*"
-
Fields to select. Formatted as in a SQL query ("*", "a, b, c"...)
- $where : string = ""
-
The where statement in the query. Formatted as in a SQL query (a = 4 AND c = 4...);
- $types : string = ""
-
String with the types. Conform to prepared statements ("ssis")
- $values : array<string|int, mixed> = []
-
The values to insert in the prepared statement
Return values
array<string|int, array<string|int, mixed>> —two dimensional array with table data
heartbeat()
Run a very lightweight query to keep the connection alive
public
heartbeat([mixed $waitForTimeout = true ]) : void
Parameters
- $waitForTimeout : mixed = true
Return values
void —mergeAsGroup()
Converts the result of the last query into a grouped array
public
mergeAsGroup(string $groupBy[, string $subElement = null ]) : array<string|int, array<string|int, mixed>>
Parameters
- $groupBy : string
-
The field, by which the array is grouped by
- $subElement : string = null
-
If set, the only a sub element of the grouped element is returned
Return values
array<string|int, array<string|int, mixed>> —Results of the last query as two dimensional array with the index as thr groupBy value
resultToArray()
Converts the result of the last query into an array and returns it
public
resultToArray([mixed $out = [] ]) : array<string|int, array<string|int, mixed>>
Parameters
- $out : mixed = []
Return values
array<string|int, array<string|int, mixed>> —Results of the last query as two dimensional array
resultToLine()
Returns one line of the last query
public
resultToLine() : array<string|int, mixed>
Return values
array<string|int, mixed> —Line of the last result