DB
in package
Database facade providing a simplified interface for common operations.
This is the main entry point for database operations in LWT 3.0+.
Usage:
use Lwt\Shared\Infrastructure\Database\DB;
// Query builder
$words = DB::table('words')->where('WoLgID', 1)->get();
// Raw queries
$result = DB::query('SELECT * FROM words WHERE WoID = 1');
$rows = DB::fetchAll('SELECT * FROM words LIMIT 10');
$row = DB::fetchOne('SELECT * FROM words WHERE WoID = 1');
$value = DB::fetchValue('SELECT COUNT(*) AS cnt FROM words', 'cnt');
// Execute non-SELECT queries
$affected = DB::execute('UPDATE words SET WoStatus = 2 WHERE WoID = 1');
// Escaping
$safe = DB::escape($userInput);
$quoted = DB::escapeString($userInput); // Returns 'escaped_value'
$nullable = DB::escapeOrNull($userInput); // Returns 'value' or NULL
Tags
Table of Contents
Methods
- beginTransaction() : bool
- Begin a transaction.
- commit() : bool
- Commit a transaction.
- connection() : mysqli
- Get the raw mysqli connection.
- escape() : string
- Escape a value for use in SQL.
- escapeOrNull() : string
- Escape and quote a string, returning 'NULL' for empty strings.
- escapeString() : string
- Escape and quote a string for SQL.
- execute() : int
- Execute an INSERT/UPDATE/DELETE query.
- fetchAll() : array<string|int, array<string|int, float|int|null|string>>
- Execute a query and return all rows.
- 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.
- 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.
- rollback() : bool
- Rollback a transaction.
- table() : QueryBuilder
- Start a query builder for a table.
Methods
beginTransaction()
Begin a transaction.
public
static beginTransaction() : bool
Return values
bool —True on success
commit()
Commit a transaction.
public
static commit() : bool
Return values
bool —True on success
connection()
Get the raw mysqli connection.
public
static connection() : mysqli
For backward compatibility and advanced operations.
Return values
mysqli —The database connection
escape()
Escape a value for use in SQL.
public
static escape(string $value) : string
Parameters
- $value : string
-
The value to escape
Return values
string —The escaped string (without quotes)
escapeOrNull()
Escape and quote a string, 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.
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.
public
static execute(string $sql) : int
Parameters
- $sql : string
-
The SQL query to execute
Return values
int —Number of affected rows
fetchAll()
Execute a query and return all rows.
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>> —Array of associative arrays
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>|null —The first row or null
fetchValue()
Execute a query and return a single value.
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
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
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>>
Parameters
- $sql : string
-
The SQL query with ? placeholders
- $params : array<int, mixed> = []
-
Parameters to bind
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
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
Return values
mysqli_result|true —Query result or true for non-SELECT queries
rollback()
Rollback a transaction.
public
static rollback() : bool
Return values
bool —True on success
table()
Start a query builder for a table.
public
static table(string $tableName) : QueryBuilder
Parameters
- $tableName : string
-
The table name (without prefix)