Connection
in package
Database connection wrapper providing a clean interface for database operations.
This class wraps mysqli and provides methods for common database operations. It uses Globals internally for backward compatibility.
Tags
Table of Contents
Properties
- $instance : mysqli|null
Methods
- escape() : string
- Escape a string for use in SQL queries.
- escapeOrNull() : string
- Escape and quote a string for SQL, returning 'NULL' for empty strings.
- escapeString() : string
- Escape and quote a string for SQL (never returns NULL).
- execute() : int|string
- Execute an INSERT/UPDATE/DELETE query and return affected rows or a message.
- fetchAll() : array<string|int, array<string|int, float|int|null|string>>
- Execute a query and return all rows as an array.
- fetchOne() : array<string|int, float|int|null|string>|null
- Execute a query and return the first row.
- fetchValue() : mixed
- Execute a query and return a single value from the first row.
- getInstance() : mysqli
- Get the database connection instance.
- isAlive() : bool
- Check if the current connection is still alive.
- lastInsertId() : int|string
- Get the last inserted ID.
- prepare() : PreparedStatement
- Create a prepared statement.
- preparedExecute() : int
- Execute a parameterized INSERT/UPDATE/DELETE query.
- preparedFetchAll() : array<int, array<string, mixed>>
- Execute a parameterized query and return all rows.
- preparedFetchOne() : array<string, mixed>|null
- Execute a parameterized query and return the first row.
- preparedFetchValue() : mixed
- Execute a parameterized query and return a single value.
- preparedInsert() : int|string
- Execute a parameterized INSERT and return the insert ID.
- query() : mysqli_result|true
- Execute a raw SQL query.
- querySelect() : mysqli_result
- Execute a SELECT query and return the result set.
- reset() : void
- Reset the connection instance (primarily for testing).
- setInstance() : void
- Set the database connection instance.
Properties
$instance
private
static mysqli|null
$instance
= null
The mysqli connection instance
Methods
escape()
Escape a string for use in SQL queries.
public
static escape(string $value) : string
Parameters
- $value : string
-
The value to escape
Return values
string —The escaped string
escapeOrNull()
Escape and quote a string for SQL, returning 'NULL' for empty strings.
public
static escapeOrNull(string $value) : string
Parameters
- $value : string
-
The value to escape
Return values
string —The escaped and quoted string, or 'NULL'
escapeString()
Escape and quote a string for SQL (never returns NULL).
public
static escapeString(string $value) : string
Parameters
- $value : string
-
The value to escape
Return values
string —The escaped and quoted string
execute()
Execute an INSERT/UPDATE/DELETE query and return affected rows or a message.
public
static execute(string $sql[, string|null $message = null ]) : int|string
Parameters
- $sql : string
-
The SQL query to execute
- $message : string|null = null
-
Optional message to return instead of affected rows count
Tags
Return values
int|string —Number of affected rows, or the message string if provided
fetchAll()
Execute a query and return all rows as an array.
public
static fetchAll(string $sql) : array<string|int, array<string|int, float|int|null|string>>
Parameters
- $sql : string
-
The SQL query to execute
Tags
Return values
array<string|int, array<string|int, float|int|null|string>>fetchOne()
Execute a query and return the first row.
public
static fetchOne(string $sql) : array<string|int, float|int|null|string>|null
Parameters
- $sql : string
-
The SQL query to execute
Tags
Return values
array<string|int, float|int|null|string>|nullfetchValue()
Execute a query and return a single value from the first row.
public
static fetchValue(string $sql[, string $column = 'value' ]) : mixed
Parameters
- $sql : string
-
The SQL query to execute
- $column : string = 'value'
-
The column name to retrieve (default: 'value')
Return values
mixed —The value or null if not found
getInstance()
Get the database connection instance.
public
static getInstance() : mysqli
Tags
Return values
mysqli —The database connection
isAlive()
Check if the current connection is still alive.
public
static isAlive() : bool
Return values
bool —True if connection is alive, false otherwise
lastInsertId()
Get the last inserted ID.
public
static lastInsertId() : int|string
Return values
int|string —The last insert ID
prepare()
Create a prepared statement.
public
static prepare(string $sql) : PreparedStatement
Parameters
- $sql : string
-
The SQL query with ? placeholders
Tags
Return values
PreparedStatement —The prepared statement wrapper
preparedExecute()
Execute a parameterized INSERT/UPDATE/DELETE query.
public
static preparedExecute(string $sql[, array<int, mixed> $params = [] ]) : int
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind
Return values
int —Number of affected rows
preparedFetchAll()
Execute a parameterized query and return all rows.
public
static preparedFetchAll(string $sql[, array<int, mixed> $params = [] ]) : array<int, array<string, mixed>>
This is a convenience method combining prepare(), bind(), and fetchAll().
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind (indexed array)
Return values
array<int, array<string, mixed>> —Array of rows
preparedFetchOne()
Execute a parameterized query and return the first row.
public
static preparedFetchOne(string $sql[, array<int, mixed> $params = [] ]) : array<string, mixed>|null
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind
Return values
array<string, mixed>|null —The first row or null
preparedFetchValue()
Execute a parameterized query and return a single value.
public
static preparedFetchValue(string $sql[, array<int, mixed> $params = [] ][, string $column = 'value' ]) : mixed
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind
- $column : string = 'value'
-
Column name to retrieve (default: 'value')
Return values
mixed —The value or null
preparedInsert()
Execute a parameterized INSERT and return the insert ID.
public
static preparedInsert(string $sql[, array<int, mixed> $params = [] ]) : int|string
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind
Return values
int|string —The last insert ID
query()
Execute a raw SQL query.
public
static query(string $sql) : mysqli_result|true
Parameters
- $sql : string
-
The SQL query to execute
Tags
Return values
mysqli_result|true —Query result or true for non-SELECT queries
querySelect()
Execute a SELECT query and return the result set.
public
static querySelect(string $sql) : mysqli_result
Use this instead of query() when you know the query returns a result set (SELECT, SHOW, DESCRIBE, EXPLAIN). This provides better type safety.
Parameters
- $sql : string
-
The SELECT query to execute
Tags
Return values
mysqli_result —Query result set
reset()
Reset the connection instance (primarily for testing).
public
static reset() : void
setInstance()
Set the database connection instance.
public
static setInstance(mysqli $connection) : void
Parameters
- $connection : mysqli
-
The mysqli connection