Documentation

PreparedStatement
in package

Wrapper class for mysqli prepared statements.

Provides a fluent interface for executing parameterized queries safely.

Usage:

// Simple query with parameters
$stmt = Connection::prepare('SELECT * FROM words WHERE WoLgID = ? AND WoStatus = ?');
$rows = $stmt->bind('ii', $langId, $status)->fetchAll();

// Insert with parameters
$stmt = Connection::prepare('INSERT INTO words (WoText, WoLgID) VALUES (?, ?)');
$stmt->bind('si', $text, $langId)->execute();
$insertId = $stmt->insertId();

// Update with parameters
$stmt = Connection::prepare('UPDATE words SET WoStatus = ? WHERE WoID = ?');
$affected = $stmt->bind('ii', $status, $wordId)->execute();
Tags
since
3.0.0

Table of Contents

Properties

$boundParams  : array<int, int|float|string|null>
$connection  : mysqli
$sql  : string
$stmt  : mysqli_stmt

Methods

__construct()  : mixed
Create a new prepared statement wrapper.
__destruct()  : mixed
Destructor - close the statement when done.
affectedRows()  : int
Get the number of affected rows from the last query.
bind()  : $this
Bind parameters to the prepared statement.
bindValues()  : $this
Bind parameters using an associative array.
close()  : void
Close the prepared statement.
execute()  : int
Execute the prepared statement.
fetchAll()  : array<int, array<string, mixed>>
Execute and fetch all rows as an associative array.
fetchColumn()  : array<int, mixed>
Execute and fetch a single column from all rows.
fetchOne()  : array<string, mixed>|null
Execute and fetch the first row.
fetchValue()  : mixed
Execute and fetch a single column value from the first row.
getStatement()  : mysqli_stmt
Get the underlying mysqli_stmt object.
insertId()  : int|string
Get the ID generated by the last INSERT query.
numRows()  : int
Get the number of rows in the result set.
normalizeParams()  : array<int, int|float|string|null>
Normalize mixed params array to list of scalars.

Properties

$boundParams

private array<int, int|float|string|null> $boundParams = []

Bound parameters for execution

Methods

__construct()

Create a new prepared statement wrapper.

public __construct(mysqli $connection, string $sql) : mixed
Parameters
$connection : mysqli

The database connection

$sql : string

The SQL query with placeholders

Tags
throws
DatabaseException

If the statement cannot be prepared

__destruct()

Destructor - close the statement when done.

public __destruct() : mixed

affectedRows()

Get the number of affected rows from the last query.

public affectedRows() : int
Return values
int

Number of affected rows, -1 on error

bind()

Bind parameters to the prepared statement.

public bind(string $types, int|float|string|null ...$params) : $this

Uses PHP 8.1's execute() with params array instead of bind_param() to avoid reference-related issues.

Parameters
$types : string

Type string (i=integer, d=double, s=string, b=blob) - kept for API compatibility

$params : int|float|string|null

Parameters to bind

Tags
throws
RuntimeException

If parameter count doesn't match

Return values
$this

For method chaining

bindValues()

Bind parameters using an associative array.

public bindValues(array<int, mixed> $params) : $this

Uses PHP 8.1's execute() with params array instead of bind_param().

Parameters
$params : array<int, mixed>

Parameters to bind (indexed array)

Return values
$this

For method chaining

execute()

Execute the prepared statement.

public execute() : int

Uses PHP 8.1's execute() with params array.

Tags
throws
DatabaseException

If execution fails

Return values
int

Number of affected rows (for INSERT/UPDATE/DELETE), -1 on error

fetchAll()

Execute and fetch all rows as an associative array.

public fetchAll() : array<int, array<string, mixed>>

Uses PHP 8.1's execute() with params array.

Return values
array<int, array<string, mixed>>

Array of rows

fetchColumn()

Execute and fetch a single column from all rows.

public fetchColumn(string $column) : array<int, mixed>
Parameters
$column : string

The column name to retrieve

Return values
array<int, mixed>

Array of values

fetchOne()

Execute and fetch the first row.

public fetchOne() : array<string, mixed>|null

Uses PHP 8.1's execute() with params array.

Return values
array<string, mixed>|null

The first row or null if no results

fetchValue()

Execute and fetch a single column value from the first row.

public fetchValue([string $column = 'value' ]) : mixed
Parameters
$column : string = 'value'

The column name to retrieve

Return values
mixed

The value or null if not found

getStatement()

Get the underlying mysqli_stmt object.

public getStatement() : mysqli_stmt

For advanced operations not covered by this wrapper.

Return values
mysqli_stmt

The underlying statement

insertId()

Get the ID generated by the last INSERT query.

public insertId() : int|string
Return values
int|string

The insert ID

numRows()

Get the number of rows in the result set.

public numRows() : int

Note: This only works after fetchAll() has been called, or after execute() for queries that return a result.

Return values
int

Number of rows (0 or more)

normalizeParams()

Normalize mixed params array to list of scalars.

private static normalizeParams(array<int, mixed> $params) : array<int, int|float|string|null>
Parameters
$params : array<int, mixed>
Return values
array<int, int|float|string|null>

        
On this page

Search results