class Database

Database interface class

Methods

__construct(string $host, string $user, string $password, string $dbName)

Create a new connection to the database

__destruct()

Destroy this connection to the database

static Database
getInstance()

Get an instance of the Database object

closeConnection()

Close the current connection to the MySQL database

bool
isConnected()

Tests whether or not the connection to the database is still active

int
getInsertId()

Get the unique row ID of the last row that was inserted

int
execute(string $queryText, mixed|array $params = false)

Prepares and executes a MySQL prepared INSERT/DELETE/UPDATE statement. The second parameter is optional when using this function to execute a query with no placeholders.

array
query(string $queryText, mixed|array $params = false)

Prepares and executes a MySQL prepared SELECT statement. The second parameter is optional when using this function to execute a query with no placeholders.

startTransaction()

Start a MySQL transaction

commit()

Commit the stored queries (usable only if a transaction has been started)

rollback()

Cancel all pending queries (does not finish the transaction

finishTransaction()

Commit all pending queries and finalise the transaction

error(string $error, int $id = null, Throwable $previous = null)

Uses monolog to log an error message

array
__sleep()

Serialize the object

Details

at line line 51
__construct(string $host, string $user, string $password, string $dbName)

Create a new connection to the database

Parameters

string $host The MySQL host
string $user The MySQL user
string $password The MySQL password for the user
string $dbName The MySQL database name

at line line 85
__destruct()

Destroy this connection to the database

at line line 99
static Database getInstance()

Get an instance of the Database object

This should be the main way to acquire access to the database

Return Value

Database The Database object

at line line 129
closeConnection()

Close the current connection to the MySQL database

at line line 139
bool isConnected()

Tests whether or not the connection to the database is still active

Return Value

bool True if the connection is active

at line line 148
int getInsertId()

Get the unique row ID of the last row that was inserted

Return Value

int The ID of the row

at line line 160
int execute(string $queryText, mixed|array $params = false)

Prepares and executes a MySQL prepared INSERT/DELETE/UPDATE statement. The second parameter is optional when using this function to execute a query with no placeholders.

Parameters

string $queryText The prepared SQL statement that will be executed
mixed|array $params (Optional) The array of values that will be binded to the prepared statement

Return Value

int Returns the number of rows affected

at line line 183
array query(string $queryText, mixed|array $params = false)

Prepares and executes a MySQL prepared SELECT statement. The second parameter is optional when using this function to execute a query with no placeholders.

Parameters

string $queryText The prepared SQL statement that will be executed
mixed|array $params (Optional) The array of values that will be binded to the prepared statement

Return Value

array Returns an array of the values received from the query

at line line 252
startTransaction()

Start a MySQL transaction

at line line 262
commit()

Commit the stored queries (usable only if a transaction has been started)

This does not show an error if there are no queries to commit

at line line 270
rollback()

Cancel all pending queries (does not finish the transaction

at line line 278
finishTransaction()

Commit all pending queries and finalise the transaction

at line line 292
error(string $error, int $id = null, Throwable $previous = null)

Uses monolog to log an error message

Parameters

string $error The error string
int $id The error ID
Throwable $previous The exception that caused the error (if any)

Exceptions

Exception

at line line 315
array __sleep()

Serialize the object

Prevents PDO from being erroneously serialized

Return Value

array The list of properties that should be serialized