DB
in package
Creates a singleton connection to the database with credentials from the config file.
Tags
Table of Contents
Properties
- $_pdo : PDO
- $_query_recorder : QueryRecorder
- $_count : int
- $_error : bool
- $_force_charset : string|null
- $_force_collation : string|null
- $_instance : DB|null
- $_prefix : string
- $_results : array<string|int, mixed>
- $_statement : PDOStatement
Methods
- addColumn() : bool
- Add a new column to a table.
- beginTransaction() : void
- Begin a MySQL transaction.
- commitTransaction() : void
- Commit a MySQL transaction.
- count() : int
- Get the number of rows affected by the last query.
- createTable() : bool
- Create a new table in the database.
- decrement() : bool
- Decrement a numeric column value by 1.
- delete() : static|false
- Perform a DELETE query on the database.
- error() : bool
- Whether there was an error during the last query.
- exists() : bool
- Get whether any results exist.
- first() : object|null
- Get the first row of the result.
- get() : static|false
- Perform a SELECT query on the database.
- getCustomInstance() : DB
- getInstance() : DB
- getPDO() : PDO
- Get the underlying PDO instance.
- increment() : bool
- Increment a numeric column value by 1.
- insert() : bool
- Insert a new row into a table within the database.
- lastId() : string|false
- Get the last inserted ID.
- makeWhere() : array<string|int, mixed>
- Convert an array of where clause data into a MySQL WHERE clause and params.
- orderAll() : static|false
- Select rows from the database, ordering by a specific column and sort type.
- orderWhere() : static|false
- Select rows from the database with a where clause, ordering by a specific column and sort type.
- query() : static
- Perform a raw SQL query on the database.
- results() : array<string|int, mixed>
- Get all the results of the query.
- rollBackTransaction() : void
- Roll back a MySQL transaction.
- showTables() : int|false
- Perform a SHOW TABLES LIKE query.
- transaction() : mixed
- Execute a database query within a MySQL transaction, and get the results of the query, if any.
- update() : bool
- Perform an UPDATE query on a table.
- __construct() : mixed
- action() : static|false
- Execute some SQL action (which uses a where clause) on the database.
Properties
$_pdo
protected
PDO
$_pdo
$_query_recorder
protected
QueryRecorder
$_query_recorder
$_count
private
int
$_count
= 0
$_error
private
bool
$_error
= \false
$_force_charset
private
string|null
$_force_charset
$_force_collation
private
string|null
$_force_collation
$_instance
private
static DB|null
$_instance
= \null
$_prefix
private
string
$_prefix
$_results
private
array<string|int, mixed>
$_results
$_statement
private
PDOStatement
$_statement
Methods
addColumn()
Add a new column to a table.
public
addColumn(string $table, string $column, string $attributes) : bool
Parameters
- $table : string
-
Name of table to alter.
- $column : string
-
The column to add.
- $attributes : string
-
The attributes of the column.
Return values
bool —Whether an error occurred or not.
beginTransaction()
Begin a MySQL transaction.
public
beginTransaction() : void
commitTransaction()
Commit a MySQL transaction.
public
commitTransaction() : void
count()
Get the number of rows affected by the last query.
public
count() : int
Return values
int —The number of rows.
createTable()
Create a new table in the database.
public
createTable(string $name, string $table_schema) : bool
Parameters
- $name : string
-
The name of the table.
- $table_schema : string
-
The table SQL schema.
Return values
bool —Whether an error occurred or not.
decrement()
Decrement a numeric column value by 1.
public
decrement(string $table, int $id, string $field) : bool
Parameters
- $table : string
-
The table to use.
- $id : int
-
The id of the row to decrement a column in.
- $field : string
-
The field to increment.
Return values
bool —Whether an error occurred or not.
delete()
Perform a DELETE query on the database.
public
delete(string $table, mixed $where) : static|false
Parameters
- $table : string
-
The table to delete from.
- $where : mixed
-
The where clause. If not an array, it will be used for "id" column lookup.
Return values
static|false —This instance if successful, false otherwise.
error()
Whether there was an error during the last query.
public
error() : bool
Return values
bool —Whether there was an error.
exists()
Get whether any results exist.
public
exists() : bool
Return values
bool —Whether any results exist.
first()
Get the first row of the result.
public
first() : object|null
Return values
object|null —The result object, or null if no result was returned.
get()
Perform a SELECT query on the database.
public
get(string $table[, mixed $where = [] ]) : static|false
Parameters
- $table : string
-
The table to select from.
- $where : mixed = []
-
The where clause. If not an array, it will be used for "id" column lookup.
Return values
static|false —This instance if successful, false otherwise.
getCustomInstance()
public
static getCustomInstance(string $host, string $database, string $username, string $password[, int $port = 3306 ][, string|null $force_charset = null ][, string|null $force_collation = null ][, string $prefix = 'nl2_' ]) : DB
Parameters
- $host : string
- $database : string
- $username : string
- $password : string
- $port : int = 3306
- $force_charset : string|null = null
- $force_collation : string|null = null
- $prefix : string = 'nl2_'
Return values
DBgetInstance()
public
static getInstance() : DB
Return values
DBgetPDO()
Get the underlying PDO instance.
public
getPDO() : PDO
Return values
PDO —The PDO instance.
increment()
Increment a numeric column value by 1.
public
increment(string $table, int $id, string $field) : bool
Parameters
- $table : string
-
The table to use.
- $id : int
-
The id of the row to increment a column in.
- $field : string
-
The field to increment.
Return values
bool —Whether an error occurred or not.
insert()
Insert a new row into a table within the database.
public
insert(string $table[, array<string|int, mixed> $fields = [] ]) : bool
Parameters
- $table : string
-
The table to insert into.
- $fields : array<string|int, mixed> = []
-
Array of data in "column => value" format to insert.
Return values
bool —Whether an error occurred or not.
lastId()
Get the last inserted ID.
public
lastId() : string|false
Return values
string|false —ID of the last inserted row or false on failure
makeWhere()
Convert an array of where clause data into a MySQL WHERE clause and params.
public
static makeWhere(array<string|int, mixed> $clauses) : array<string|int, mixed>
Parameters
- $clauses : array<string|int, mixed>
-
An array, or nested array, of column, operator (default =), value, and glue (default AND).
Return values
array<string|int, mixed> —The where clause string, and parameters to bind.
orderAll()
Select rows from the database, ordering by a specific column and sort type.
public
orderAll(string $table, string $order, string $sort) : static|false
Parameters
- $table : string
-
The table to use.
- $order : string
-
The column to order by.
- $sort : string
-
ASC or DESC
Return values
static|false —This instance if successful, false otherwise.
orderWhere()
Select rows from the database with a where clause, ordering by a specific column and sort type.
public
orderWhere(string $table, string $where, string $order, string $sort) : static|false
Parameters
- $table : string
-
The table to use.
- $where : string
- $order : string
-
The column to order by.
- $sort : string
-
ASC or DESC
Return values
static|false —This instance if successful, false otherwise.
query()
Perform a raw SQL query on the database.
public
query(string $sql[, array<string|int, mixed> $params = [] ][, bool $isSelect = null ]) : static
Parameters
- $sql : string
-
The SQL query string to execute.
- $params : array<string|int, mixed> = []
-
The parameters to bind to the query.
- $isSelect : bool = null
-
Whether the statement is a select, defaults to null
Return values
static —This DB instance.
results()
Get all the results of the query.
public
results() : array<string|int, mixed>
Return values
array<string|int, mixed> —The results of the query.
rollBackTransaction()
Roll back a MySQL transaction.
public
rollBackTransaction() : void
showTables()
Perform a SHOW TABLES LIKE query.
public
showTables(string $table) : int|false
Parameters
- $table : string
-
Name of table to try and lookup.
Return values
int|false —The number of rows affected, or false on failure.
transaction()
Execute a database query within a MySQL transaction, and get the results of the query, if any.
public
transaction(callable(DB): mixed $closure) : mixed
Parameters
- $closure : callable(DB): mixed
-
The closure to pass this instance to and execute within a transaction context.
Return values
mixed —The results of the query, null if none.
update()
Perform an UPDATE query on a table.
public
update(string $table, mixed $where, array<string|int, mixed> $fields) : bool
Parameters
- $table : string
-
The table to update.
- $where : mixed
-
The where clause. If not an array, it will be used for "id" column lookup.
- $fields : array<string|int, mixed>
-
Array of data in "column => value" format to update.
Return values
bool —Whether an error occurred or not.
__construct()
private
__construct(string $host, string $database, string $username, string $password, int $port, string|null $force_charset, string|null $force_collation, string $prefix) : mixed
Parameters
- $host : string
- $database : string
- $username : string
- $password : string
- $port : int
- $force_charset : string|null
- $force_collation : string|null
- $prefix : string
action()
Execute some SQL action (which uses a where clause) on the database.
private
action(string $action, string $table[, array<string|int, mixed> $where = [] ]) : static|false
Parameters
- $action : string
-
The action to perform (SELECT, DELETE).
- $table : string
-
The table to perform the action on.
- $where : array<string|int, mixed> = []
-
The where clause.
Return values
static|false —This instance if successful, false otherwise.